-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBJ_2573.java
136 lines (121 loc) · 3.88 KB
/
BJ_2573.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.io.*;
import java.util.*;
class Node {
int x, y, height;
public Node(int x, int y, int height) {
this.x = x;
this.y = y;
this.height = height;
}
}
class BJ_2573 {
static int N, M, n;
static int[][] arr;
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, -1, 0, 1};
static boolean[][] visited;
static int[][] ice;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(reader.readLine());
N = Integer.parseInt(token.nextToken());
M = Integer.parseInt(token.nextToken());
arr = new int[N][M];
visited = new boolean[N][M];
ice = new int[N][M];
for(int i=0; i<N; i++) {
token = new StringTokenizer(reader.readLine());
for(int j=0; j<M; j++) {
arr[i][j] = Integer.parseInt(token.nextToken());
}
}
int result = 0;
while(true) {
visited = new boolean[N][M];
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
if(arr[i][j] != 0 && !visited[i][j]) {
BFS(i, j);
}
}
}
n=1;
visited = new boolean[N][M];
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
if(arr[i][j] != 0 && !visited[i][j]) {
findMass(i, j);
n++;
}
}
}
result++;
// 덩어리로 나눠진 경우
if(n>2) {
break;
}
// 녹을 떄까지 나눠지지 않은 경우
if(n == 1) {
System.out.println(0);
System.exit(0);
}
}
System.out.println(result);
}
public static void findMass(int i, int j) {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(i, j, arr[i][j]));
visited[i][j] = true;
ice[i][j] = n;
while(!queue.isEmpty()) {
Node now = queue.poll();
for(int d=0; d<4; d++) {
int x = now.x + dx[d];
int y = now.y + dy[d];
// 범위를 벗어나는 경우
if(x<0 || x>=N || y<0 || y>=M) {
continue;
}
if(arr[x][y] != 0 && !visited[x][y]) {
ice[x][y] = n;
visited[x][y] = true;
queue.add(new Node(x, y, arr[x][y]));
}
}
}
}
public static void BFS(int i, int j) {
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(i, j, arr[i][j]));
visited[i][j] = true;
while(!queue.isEmpty()) {
Node now = queue.poll();
int count=0;
for(int d=0; d<4; d++) {
int x = now.x + dx[d];
int y = now.y + dy[d];
// 범위를 벗어나는 경우
if(x<0 || x>=N || y<0 || y>=M) {
continue;
}
// 상하좌우 중 바다인 경우
if(arr[x][y] == 0 && !visited[x][y]) {
count++;
}
// 상하좌우 중 빙산인 경우
else {
if(!visited[x][y]) {
visited[x][y] = true;
ice[x][y] = n;
queue.add(new Node(x, y, arr[x][y]));
}
}
}
arr[now.x][now.y] -= count;
if(arr[now.x][now.y] <= 0) {
arr[now.x][now.y] = 0;
ice[now.x][now.y] = 0;
}
}
}
}