From d0ca5f5f059897ba9486d3697713d90fad0a1b36 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:01:22 +0900 Subject: [PATCH] =?UTF-8?q?[20250730]=20BOJ=20/=20G5=20/=20=EB=86=8D?= =?UTF-8?q?=EC=9E=A5=20=EA=B4=80=EB=A6=AC=20/=20=EA=B9=80=EC=88=98?= =?UTF-8?q?=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\236\245 \352\264\200\353\246\254.md" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "suyeun84/202507/30 BOJ G5 \353\206\215\354\236\245 \352\264\200\353\246\254.md" diff --git "a/suyeun84/202507/30 BOJ G5 \353\206\215\354\236\245 \352\264\200\353\246\254.md" "b/suyeun84/202507/30 BOJ G5 \353\206\215\354\236\245 \352\264\200\353\246\254.md" new file mode 100644 index 00000000..bd16caf7 --- /dev/null +++ "b/suyeun84/202507/30 BOJ G5 \353\206\215\354\236\245 \352\264\200\353\246\254.md" @@ -0,0 +1,67 @@ +```java +import java.util.*; +import java.io.*; + +public class boj1245 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int N, M, answer; + static int[][] map; + static boolean[][] visited, top; + static int[][] dir = {{1,0}, {0,1}, {0,-1}, {-1,0}, {-1,1}, {-1,-1}, {1,1}, {1,-1}}; + public static void main(String[] args) throws Exception { + nextLine(); + N = nextInt(); + M = nextInt(); + map = new int[N][M]; + top = new boolean[N][M]; + for (int i = 0; i < N; i++) { + nextLine(); + for (int j = 0; j < M; j++) map[i][j] = nextInt(); + } + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] == 0 || top[i][j]) continue; + bfs(i, j); + } + } + System.out.println(answer); + } + static void bfs(int y, int x) { + Queue q = new LinkedList<>(); + boolean[][] visited = new boolean[N][M]; + LinkedList topList = new LinkedList<>(); + q.add(new Pos(y, x)); + visited[y][x] = true; + while (!q.isEmpty()) { + Pos cur = q.poll(); + for (int[] d : dir) { + int ny = cur.y + d[0]; + int nx = cur.x + d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M || visited[ny][nx]) continue; + visited[ny][nx] = true; + if (map[ny][nx] > map[cur.y][cur.x]) return; + else if (map[ny][nx] == map[cur.y][cur.x]) { + q.add(new Pos(ny, nx)); + topList.add(new Pos(ny, nx)); + } + } + } + for (Pos pos : topList) { + top[pos.y][pos.x] = true; + } + answer++; + } + + static class Pos { + int y, x; + public Pos(int y, int x) { + this.y = y; + this.x = x; + } + } +} +```