Skip to content

Commit a5c87aa

Browse files
authored
Merge pull request #869 from AlgorithmWithGod/0224LJH
[20250911] BOJ / G5 / 농장 관리 / 이종환
2 parents 5101745 + 01891ab commit a5c87aa

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
public class Main {
8+
static int height, width, ans;
9+
static int[][] arr;
10+
static boolean[][] visited;
11+
static boolean isTop;
12+
13+
static int[] dy = {-1,-1,0,1,1,1,0,-1};
14+
static int[] dx = {0,1,1,1,0,-1,-1,-1};
15+
16+
17+
public static void main(String[] args) throws NumberFormatException, IOException {
18+
init();
19+
process();
20+
print();
21+
}
22+
23+
public static void init() throws NumberFormatException, IOException {
24+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
height = Integer.parseInt(st.nextToken());
27+
width = Integer.parseInt(st.nextToken());
28+
29+
arr = new int[height][width];
30+
visited = new boolean[height][width];
31+
32+
for (int i = 0; i < height; i++) {
33+
st = new StringTokenizer(br.readLine());
34+
for (int j = 0; j < width; j++) {
35+
arr[i][j] =Integer.parseInt(st.nextToken());
36+
}
37+
}
38+
}
39+
40+
public static void process() {
41+
for (int i = 0; i < height; i++) {
42+
for (int j = 0; j < width; j++) {
43+
if (!visited[i][j]) {
44+
isTop = true;
45+
check(i,j);
46+
if (isTop) ans++;
47+
}
48+
}
49+
}
50+
51+
}
52+
53+
public static void check(int y, int x) {
54+
int num = arr[y][x];
55+
visited[y][x] = true;
56+
for (int i = 0; i < 8; i++) {
57+
int ny = dy[i] + y;
58+
int nx = dx[i] + x;
59+
if (ny < 0 || nx < 0 || ny >= height || nx >= width) continue;
60+
61+
int nNum = arr[ny][nx];
62+
63+
if (nNum < num) continue;
64+
else if (nNum == num ) {
65+
if (!visited[ny][nx])check(ny,nx);
66+
}
67+
else {
68+
isTop = false;
69+
continue;
70+
}
71+
}
72+
}
73+
74+
75+
76+
77+
public static void print() {
78+
System.out.println(ans);
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)