-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_7576.java
More file actions
72 lines (61 loc) · 2.19 KB
/
BOJ_7576.java
File metadata and controls
72 lines (61 loc) · 2.19 KB
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.*;
import java.util.*;
public class BOJ_7576 {
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int m = Integer.parseInt(st.nextToken()); // col
int n = Integer.parseInt(st.nextToken()); // row
Deque<int[]> q = new ArrayDeque<>();
// 1: 익은 토마토, 0: 익지 않은 토마토, -1: 토마토 없음
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
int num = Integer.parseInt(st.nextToken());
if (num == 1) {
q.add(new int[]{i, j, 0});
}
arr[i][j] = num;
}
}
int max = Integer.MIN_VALUE;
while (!q.isEmpty()) {
int[] cur = q.poll();
int r = cur[0], c = cur[1], depth = cur[2];
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
// 범위 나가면 무시
if (nr < 0 || nr >= n || nc < 0 || nc >= m) {
continue;
}
// 이미 익었다면 건너 뜀
if (arr[nr][nc] == 1) {
continue;
}
if (arr[nr][nc] == 0) {
q.add(new int[]{nr, nc, depth + 1});
arr[nr][nc] = 1; // 방문 처리
max = depth + 1;
}
}
}
for (int[] row : arr) {
for (int col : row) {
if (col == 0) {
System.out.println(-1);
System.exit(0);
}
}
}
// 다 처리하고 보니 처음부터 익은 토마토와 벽 밖에 없었을 떄
if (max == Integer.MIN_VALUE) {
System.out.println(0);
System.exit(0);
}
System.out.println(max);
}
}