• 문제 링크
https://www.acmicpc.net/problem/8783
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
static boolean[][] mat;
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int z = Integer.parseInt(br.readLine());
while (z-- > 0) {
int n = Integer.parseInt(br.readLine());
mat = new boolean[n + 2][n + 2];
for (int i = 1; i <= n; i++) {
String s = br.readLine();
for (int j = 1; j <= n; j++) mat[i][j] = s.charAt(j - 1) == '#';
}
sb.append(bfs(new Pair(0, 0))).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
private static int bfs(Pair src) {
Queue<Pair> q = new ArrayDeque<>();
q.offer(src);
mat[src.y][src.x] = true;
int cnt = mat.length * mat.length;
while (!q.isEmpty()) {
Pair cur = q.poll();
cnt--;
for (int[] d : dir) {
int ny = cur.y + d[0], nx = cur.x + d[1];
if (ny < 0 || ny >= mat.length || nx < 0 || nx >= mat.length || mat[ny][nx]) continue;
q.offer(new Pair(ny, nx));
mat[ny][nx] = true;
}
}
return cnt;
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 8620 Hurtownia - Data Structure / Java (0) | 2024.06.22 |
---|---|
[백준] 6149 Card Stacking - Data Structure / Java (0) | 2024.06.21 |
[백준] 6847 Friends - Graph Theory / Java (0) | 2024.06.19 |
[백준] 30610 A-maze-ing Lakes - Graph Theory / Java (0) | 2024.06.18 |
[백준] 26942 Gruppindelning - Graph Theory / Java (0) | 2024.06.17 |
댓글