From a2a54ed3fcf40d9d906b56215b72e26c4bdccd8e Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Sat, 13 Sep 2025 15:39:54 +0900 Subject: [PATCH] =?UTF-8?q?[20250913]=20PGM=20/=20LV2=20/=20=EB=A6=AC?= =?UTF-8?q?=EC=BD=94=EC=B3=87=20=EB=A1=9C=EB=B4=87=20/=20=EA=B9=80?= =?UTF-8?q?=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\263\207 \353\241\234\353\264\207.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "suyeun84/202509/13 PGM LV2 \353\246\254\354\275\224\354\263\207 \353\241\234\353\264\207.md" diff --git "a/suyeun84/202509/13 PGM LV2 \353\246\254\354\275\224\354\263\207 \353\241\234\353\264\207.md" "b/suyeun84/202509/13 PGM LV2 \353\246\254\354\275\224\354\263\207 \353\241\234\353\264\207.md" new file mode 100644 index 00000000..e227062f --- /dev/null +++ "b/suyeun84/202509/13 PGM LV2 \353\246\254\354\275\224\354\263\207 \353\241\234\353\264\207.md" @@ -0,0 +1,68 @@ +```java +import java.util.*; +class Solution { + static int[][] dir = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}}; + static int answer = -1; + static Point start, target; + static int N, M; + static char[][] map; + static int[][] visited; + + public int solution(String[] board) { + N = board.length; + M = board[0].length(); + map = new char[N][M]; + visited = new int[N][M]; + for (int i = 0; i < N; i++) { + map[i] = board[i].toCharArray(); + Arrays.fill(visited[i], Integer.MAX_VALUE); + for (int j = 0; j < M; j++) { + if (map[i][j] == 'R') start = new Point(i, j, 0); + else if (map[i][j] == 'G') target = new Point(i, j, 0); + } + } + + bfs(); + + return answer; + } + + private void bfs() { + Queue q = new LinkedList<>(); + q.add(new Point(start.y, start.x, start.cnt)); + visited[start.y][start.x] = 0; + while (!q.isEmpty()) { + Point curr = q.poll(); + if (curr.y == target.y && curr.x == target.x) { + answer = curr.cnt; + return; + } + + for (int[] d : dir) { + int ny = curr.y, nx = curr.x; + while (true) { + ny += d[0]; + nx += d[1]; + if (ny < 0 || ny >= N || nx < 0 || nx >= M || map[ny][nx] == 'D') { + ny -= d[0]; + nx -= d[1]; + if (visited[ny][nx] <= curr.cnt+1) break; + q.add(new Point(ny, nx, curr.cnt+1)); + visited[ny][nx] = curr.cnt+1; + } + } + } + } + + } + + class Point { + int y, x, cnt; + public Point(int y, int x, int cnt) { + this.y = y; + this.x = x; + this.cnt = cnt; + } + } +} +```