Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Ukj0ng/202601/20 BOJ G5 농장 수열.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
```
import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private static final int[] dx = {1, 0, -1, 0, 1, 1, -1, -1};
private static final int[] dy = {0, 1, 0, -1, 1, -1, 1, -1};
private static int[][] arr;
private static boolean[][] visited;
private static int N, M;
private static boolean isPeak;

public static void main(String[] args) throws IOException {
init();
int answer = 0;

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] > 0 && !visited[i][j]) {
isPeak = true;
BFS(i, j);
if (isPeak) {
answer++;
}
}
}
}

bw.write(answer + "\n");
bw.flush();
bw.close();
br.close();
}

private static void init() throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

arr = new int[N][M];
visited = new boolean[N][M];

for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
}

private static void BFS(int x, int y) {
Queue<int[]> q = new ArrayDeque<>();
visited[x][y] = true;
q.add(new int[]{x, y});

while (!q.isEmpty()) {
int[] current = q.poll();
int cx = current[0];
int cy = current[1];

for (int i = 0; i < 8; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];

if (OOB(nx, ny)) continue;

if (arr[nx][ny] > arr[cx][cy]) {
isPeak = false;
}

if (arr[nx][ny] == arr[cx][cy] && !visited[nx][ny]) {
visited[nx][ny] = true;
q.add(new int[]{nx, ny});
}
}
}
}

private static boolean OOB(int nx, int ny) {
return nx < 0 || nx > N-1 || ny < 0 || ny > M-1;
}
}
```