From 5f3a0472e97d25d382c3fac2aaac04dc01fe4279 Mon Sep 17 00:00:00 2001 From: LiiNi-coder <97495437+LiiNi-coder@users.noreply.github.com> Date: Thu, 25 Sep 2025 23:45:08 +0900 Subject: [PATCH] =?UTF-8?q?[20250925]=20PGM=20/=20LV2=20/=20=EB=A6=AC?= =?UTF-8?q?=EC=BD=94=EC=B1=97=20=EB=A1=9C=EB=B4=87=20/=20=EC=9D=B4?= =?UTF-8?q?=EC=9D=B8=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\261\227 \353\241\234\353\264\207.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "LiiNi-coder/202509/25 PGM \353\246\254\354\275\224\354\261\227 \353\241\234\353\264\207.md" diff --git "a/LiiNi-coder/202509/25 PGM \353\246\254\354\275\224\354\261\227 \353\241\234\353\264\207.md" "b/LiiNi-coder/202509/25 PGM \353\246\254\354\275\224\354\261\227 \353\241\234\353\264\207.md" new file mode 100644 index 00000000..21964937 --- /dev/null +++ "b/LiiNi-coder/202509/25 PGM \353\246\254\354\275\224\354\261\227 \353\241\234\353\264\207.md" @@ -0,0 +1,73 @@ +```java +import java.util.*; + +class Solution { + int[] Dr = {-1, 1, 0, 0}; + int[] Dc = {0, 0, -1, 1}; + int Rr, Rc, Gr, Gc; + int H; + int W; + boolean[][] visited; + + public int solution(String[] board) { + + H = board.length; + W = board[0].length(); + visited = new boolean[H][W]; + + for (int i = 0; i < H; i++) { + for (int j = 0; j < W; j++) { + char c = board[i].charAt(j); + if (c == 'R') { + Rr = i; + Rc = j; + } + if (c == 'G') { + Gr = i; + Gc = j; + } + } + } + Queue q = new ArrayDeque<>(); + q.add(new int[]{Rr, Rc, 0}); + visited[Rr][Rc] = true; + + while (!q.isEmpty()) { + int[] cur = q.poll(); + int r = cur[0]; + int c = cur[1]; + int d = cur[2]; + if (r == Gr && c == Gc) { + return d; + } + for (int i = 0; i < 4; i++) { + int[] next = slide(r, c, i, board); + int nr = next[0], nc = next[1]; + if (visited[nr][nc]) + continue; + + visited[nr][nc] = true; + q.add(new int[]{nr, nc, d + 1}); + } + } + + return -1; + } + + int[] slide(int r, int c, int direction, String[] board) { + int nr = r, nc = c; + while (true) { + int tr = nr + Dr[direction]; + int tc = nc + Dc[direction]; + if (tr < 0 || tr >= H || tc < 0 || tc >= W) + break; + if (board[tr].charAt(tc) == 'D') + break; + nr = tr; + nc = tc; + } + return new int[]{nr, nc}; + } +} + +```