-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP075_BJ2636_치즈.java
72 lines (58 loc) · 1.47 KB
/
P075_BJ2636_치즈.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
// bfs
public class P075_BJ2636_치즈 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int total = 0;
int[] dr = {-1, 1, 0, 0};
int[] dc = {0, 0, -1, 1};
int R = Integer.parseInt(input[0]);
int C = Integer.parseInt(input[1]);
int[][] map = new int[R][C];
// 입력받기
for (int r = 0; r < R; r++) {
input = br.readLine().split(" ");
for (int c = 0; c < C; c++) {
map[r][c] = Integer.parseInt(input[c]);
if (map[r][c] == 1) {
total++;
}
}
}
int time = 0;
int cnt = total;
while(cnt > 0) {
// bfs
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[] {0, 0});
boolean[][] visited = new boolean[R][C];
visited[0][0] = true;
while(!q.isEmpty()) {
int[] cur = q.poll();
for (int d = 0; d < 4; d++) {
int nr = cur[0] + dr[d];
int nc = cur[1] + dc[d];
if (nr < 0 || nr >= R || nc < 0 || nc >= C) continue;
if (visited[nr][nc]) continue;
if (map[nr][nc] == 0) {
q.offer(new int[] {nr, nc});
} else {
map[nr][nc] = 0;
cnt--;
}
visited[nr][nc] = true;
}
}
time++;
if (cnt != 0) {
total = cnt;
}
}
System.out.println(time);
System.out.println(total);
}
}