https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LwsHaD1MDFAXc
package day6;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solution_d4_1868_파핑파핑지뢰찾기 {
static int N, ans;
static char[][] map;
static int[][] visit;
static int[] dx = { 0, 0, 1, -1, 1, 1, -1, -1 };
static int[] dy = { 1, -1, 0, 0, 1, -1, 1, -1 };
static Queue<int[]> q;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
q = new LinkedList<int[]>();
N = Integer.parseInt(br.readLine());
map = new char[N][N];
visit = new int[N][N];
ans = 0;
for (int i = 0; i < N; i++) {
map[i] = br.readLine().toCharArray();
} // 입력끝
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] == '.' && visit[i][j] == 0) {
int mines = 0;
for (int k = 0; k < 8; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
continue;
if (map[nx][ny] == '*')
mines++;
}
if (mines == 0) {
visit[i][j] = 1;
q.offer(new int[] { i, j });
bfs();
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(map[i][j]=='.') {
ans++;
}
}
}
System.out.println("#" + tc + " " + ans);
}
}
private static void bfs() {
ans++;
while (!q.isEmpty()) {
Queue<int[]> tmp = new LinkedList<int[]>();
int[] a = q.poll();
int x = a[0];
int y = a[1];
int mines = 0;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
continue;
if (map[nx][ny] == '*')
mines++;
else if (map[nx][ny] == '.' && visit[nx][ny] == 0) {
tmp.offer(new int[] { nx, ny });
}
}
if (mines == 0) {
map[x][y] = 'x';
while (!tmp.isEmpty()) {
int[] arr = tmp.poll();
visit[arr[0]][arr[1]] = 1;
q.offer(arr);
}
} else {
map[x][y] = 'x';
}
}
}
}
'알고리즘 문제풀이 > SWEA' 카테고리의 다른 글
[SWEA 4012] 요리사(C++) (0) | 2020.03.16 |
---|---|
[SWEA 3378] 스타일리쉬 들여쓰기(JAVA) (2) | 2020.03.13 |
[SWEA 7701] 염라대왕의 이름 정렬(JAVA) (2) | 2020.03.12 |
[SWEA 1251] 하나로(JAVA/C++) (0) | 2020.03.11 |
[SWEA 7396] 종구의 딸이름 짓기(JAVA) (0) | 2020.03.10 |
댓글