-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.java
121 lines (80 loc) · 2.88 KB
/
Main.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* BOJ#2468 안전영역
* https://www.acmicpc.net/problem/2468
*/
public class Main {
static final int[] dRow = {0, -1, 0, 1};
static final int[] dCol = {-1, 0, 1, 0};
static int[][] map = new int[101][101];
static boolean[][] visited = new boolean[101][101];
static int minValue = Integer.MAX_VALUE;
static int maxValue = 0;
public static void main(String[] args) throws IOException {
int N;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
int a = Integer.parseInt(st.nextToken());
map[i][j] = a;
maxValue = maxValue < a ? a : maxValue;
minValue = minValue > a ? a : minValue;
}
}
// solve
int ans = 1;
while (minValue <= maxValue) {
int tempAns = 0;
visitedInit(N);
Queue<Node> queue = new LinkedList<Node>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && map[i][j] > minValue) {
queue.add(new Node(i, j));
visited[i][j] = true;
tempAns++;
while (!queue.isEmpty()) {
Node node = queue.poll();
for (int k = 0; k < 4; k++) {
int nextRow = node.row + dRow[k];
int nextCol = node.col + dCol[k];
if (nextRow < 0 || nextRow >= N || nextCol < 0 || nextCol >= N) continue;
// 침수 지역이면
if (map[nextRow][nextCol] <= minValue || visited[nextRow][nextCol]) {
continue;
}
queue.add(new Node(nextRow, nextCol));
visited[nextRow][nextCol] = true;
}
}
}
}
}
ans = ans < tempAns ? tempAns : ans;
minValue++;
}
System.out.println(ans);
}
static void visitedInit(int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
visited[i][j] = false;
}
}
}
}
class Node {
int row;
int col;
Node(int row, int col) {
this.row = row;
this.col = col;
}
}