Skip to content

Commit a461503

Browse files
authored
Merge pull request #1805 from AlgorithmWithGod/Ukj0ng
[20260120] BOJ / G5 / 농장 관리 / 한종욱
2 parents d53d828 + 6778ab7 commit a461503

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```
2+
import java.io.*;
3+
import java.util.ArrayDeque;
4+
import java.util.Queue;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static final int[] dx = {1, 0, -1, 0, 1, 1, -1, -1};
11+
private static final int[] dy = {0, 1, 0, -1, 1, -1, 1, -1};
12+
private static int[][] arr;
13+
private static boolean[][] visited;
14+
private static int N, M;
15+
private static boolean isPeak;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
int answer = 0;
20+
21+
for (int i = 0; i < N; i++) {
22+
for (int j = 0; j < M; j++) {
23+
if (arr[i][j] > 0 && !visited[i][j]) {
24+
isPeak = true;
25+
BFS(i, j);
26+
if (isPeak) {
27+
answer++;
28+
}
29+
}
30+
}
31+
}
32+
33+
bw.write(answer + "\n");
34+
bw.flush();
35+
bw.close();
36+
br.close();
37+
}
38+
39+
private static void init() throws IOException {
40+
StringTokenizer st = new StringTokenizer(br.readLine());
41+
N = Integer.parseInt(st.nextToken());
42+
M = Integer.parseInt(st.nextToken());
43+
44+
arr = new int[N][M];
45+
visited = new boolean[N][M];
46+
47+
for (int i = 0; i < N; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
for (int j = 0; j < M; j++) {
50+
arr[i][j] = Integer.parseInt(st.nextToken());
51+
}
52+
}
53+
}
54+
55+
private static void BFS(int x, int y) {
56+
Queue<int[]> q = new ArrayDeque<>();
57+
visited[x][y] = true;
58+
q.add(new int[]{x, y});
59+
60+
while (!q.isEmpty()) {
61+
int[] current = q.poll();
62+
int cx = current[0];
63+
int cy = current[1];
64+
65+
for (int i = 0; i < 8; i++) {
66+
int nx = cx + dx[i];
67+
int ny = cy + dy[i];
68+
69+
if (OOB(nx, ny)) continue;
70+
71+
if (arr[nx][ny] > arr[cx][cy]) {
72+
isPeak = false;
73+
}
74+
75+
if (arr[nx][ny] == arr[cx][cy] && !visited[nx][ny]) {
76+
visited[nx][ny] = true;
77+
q.add(new int[]{nx, ny});
78+
}
79+
}
80+
}
81+
}
82+
83+
private static boolean OOB(int nx, int ny) {
84+
return nx < 0 || nx > N-1 || ny < 0 || ny > M-1;
85+
}
86+
}
87+
```

0 commit comments

Comments
 (0)