Repository navigation
Expand file tree
/
Copy pathMain_2468.java
More file actions
92 lines (76 loc) · 1.95 KB
/
Copy pathMain_2468.java
File metadata and controls
92 lines (76 loc) · 1.95 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package baekjoon;
import java.io.*;
import java.util.*;
/*
* 안전영역
* */
public class Main_2468 {
static int n;
static int[][] board;
static int[][] watered; // 물에 잠긴 영역을 표시하기 위한 배열
static int[] dx = {0, 0, 1, -1};
static int[] dy = {1, -1, 0, 0};
static int answer = Integer.MIN_VALUE;
static class Pointer {
int x;
int y;
public Pointer(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void BFS(Pointer pointer) {
Queue<Pointer> queue = new LinkedList<>();
queue.offer(pointer);
while (!queue.isEmpty()) {
Pointer current = queue.poll();
for (int i = 0; i < dx.length; i++) {
int nx = current.x + dx[i];
int ny = current.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < n && watered[nx][ny] == 0) {
watered[nx][ny] = 1;
queue.offer(new Pointer(nx, ny));
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(br.readLine());
board = new int[n][n];
ArrayList<Integer> numberList = new ArrayList<>();
numberList.add(0);
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
if (!numberList.contains(board[i][j])) {
numberList.add(board[i][j]);
}
}
}
Collections.sort(numberList);
for (int number : numberList) {
watered = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] <= number) {
watered[i][j] = 1;
}
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (watered[i][j] == 0) { // 물에 잠기지 않은 영역 bfs로 체크
Main_2468.BFS(new Pointer(i, j));
count++;
}
}
}
answer = Math.max(answer, count);
}
System.out.println(answer);
}
}