-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudy_7576.java
More file actions
78 lines (70 loc) · 1.56 KB
/
Study_7576.java
File metadata and controls
78 lines (70 loc) · 1.56 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
73
74
75
76
77
78
import java.util.*;
// Create by Inho 2018. 4. 9. 오전 8:56:17
// 7576 토마토
public class Study_7576 {
static int[] dx = {0,0,1,-1};
static int[] dy = {1,-1,0,0};
static class Tomato{
int x,y;
Tomato(int x,int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
scanner.nextLine();
int[][] Box = new int[N][M];
Queue<Tomato> q = new LinkedList<Tomato>();
Tomato[] tomato = new Tomato[N*M];
int count =0;
int max =0;
for(int i = 0; i< N ;i++){
for(int j = 0 ;j<M;j++){
Box[i][j] = scanner.nextInt();
if(Box[i][j] == 1){
tomato[count++] = new Tomato(j,i);
q.add(tomato[count-1]);
}
}
scanner.nextLine();
}
while(!q.isEmpty()){
int x = q.peek().x;
int y = q.peek().y;
q.poll();
for(int j=0;j<4;j++){
int next_x = x+dx[j];
int next_y = y+dy[j];
if(next_x >= 0 && next_y >=0 && next_x <M && next_y <N){
if(Box[next_y][next_x] == 0){
Box[next_y][next_x] = Box[y][x]+1;
q.add(new Tomato(next_x,next_y));
}else{
if(Box[next_y][next_x] > Box[y][x]+1){
Box[next_y][next_x] = Box[y][x]+1;
q.add(new Tomato(next_x,next_y));
}
}
}
}
}
for(int i = 0; i< N ;i++){
for(int j = 0 ;j<M;j++){
if( max < Box[i][j]){
max = Box[i][j];
}
}
}
for(int i = 0; i< N ;i++){
for(int j = 0 ;j<M;j++){
if( Box[i][j] == 0){
max = 0;
}
}
}
System.out.println(max-1);
}
}