-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon7576.java
107 lines (87 loc) · 2.82 KB
/
Baekjoon7576.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/7576
* 백준 7576번 토마토
*/
public class Baekjoon7576 {
private static int[][] box;
private static int[] dx = {1, 0, -1, 0};
private static int[][] visited;
private static int[] dy = {0, 1, 0, -1};
private static Queue<Node> queue = new LinkedList<>();
private static Queue<Node> waitingQueue = new LinkedList<>();
public static void main(String[] args) throws IOException {
getInputData();
int days = 0;
while (!isAllRipe()) {
int changed = dayPass();
if (changed == 0) {
System.out.println(-1);
return;
}
days++;
}
System.out.println(days);
}
private static int dayPass() {
int check = 0;
while(!waitingQueue.isEmpty()){
Node top = waitingQueue.poll();
visited[top.x][top.y] = 1;
queue.add(top);
}
while (!queue.isEmpty()) {
Node now = queue.poll();
for (int i = 0; i < 4; i++) {
int nextX = now.x + dx[i];
int nextY = now.y + dy[i];
if (nextX < 0 || nextY < 0 || nextX >= box.length || nextY >= box[0].length) continue;
if (visited[nextX][nextY] == 1) continue;
if (box[nextX][nextY] == 0) {
check++;
box[nextX][nextY] = 1;
waitingQueue.add(new Node(nextX, nextY));
}
}
}
return check;
}
private static boolean isAllRipe() {
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[0].length; j++) {
if (box[i][j] == 0) return false;
}
}
return true;
}
private static void getInputData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
box = new int[M][N];
visited = new int[M][N];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
box[i][j] = Integer.parseInt(st.nextToken());
if(box[i][j] == 1){
waitingQueue.add(new Node(i, j));
}
}
}
}
private static class Node {
int x, y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}