From 3289a2ce9b337ccb3f7a33b93f1e03bb7289c82f Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 20 Nov 2025 16:21:11 +0900 Subject: [PATCH] =?UTF-8?q?[20251120]=20BOJ=20/=20G5=20/=20=EB=86=8D?= =?UTF-8?q?=EC=9E=A5=EA=B4=80=EB=A6=AC=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\354\236\245\352\264\200\353\246\254.md" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "JHLEE325/202511/20 BOJ G5 \353\206\215\354\236\245\352\264\200\353\246\254.md" diff --git "a/JHLEE325/202511/20 BOJ G5 \353\206\215\354\236\245\352\264\200\353\246\254.md" "b/JHLEE325/202511/20 BOJ G5 \353\206\215\354\236\245\352\264\200\353\246\254.md" new file mode 100644 index 00000000..1ca4ede0 --- /dev/null +++ "b/JHLEE325/202511/20 BOJ G5 \353\206\215\354\236\245\352\264\200\353\246\254.md" @@ -0,0 +1,78 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int N, M; + static int[][] map; + static boolean[][] visited; + static int[] dr = {-1,-1,-1,0,0,1,1,1}; + static int[] dc = {-1,0,1,-1,1,-1,0,1}; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = 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++) { + map[i][j] = Integer.parseInt(st.nextToken()); + } + } + + int count = 0; + + for (int r = 0; r < N; r++) { + for (int c = 0; c < M; c++) { + if (!visited[r][c]) { + if (bfs(r, c)) { + count++; + } + } + } + } + + System.out.println(count); + } + + static boolean bfs(int sr, int sc) { + Queue q = new LinkedList<>(); + visited[sr][sc] = true; + q.add(new int[]{sr, sc}); + + int height = map[sr][sc]; + boolean isPeak = true; + + while (!q.isEmpty()) { + int[] cur = q.poll(); + int r = cur[0]; + int c = cur[1]; + + for (int d = 0; d < 8; d++) { + int nr = r + dr[d]; + int nc = c + dc[d]; + + if (nr < 0 || nc < 0 || nr >= N || nc >= M) continue; + + if (map[nr][nc] > height) { + isPeak = false; + } + + if (map[nr][nc] == height && !visited[nr][nc]) { + visited[nr][nc] = true; + q.add(new int[]{nr, nc}); + } + } + } + + return isPeak; + } +} +```