From 1664d1e270906659ae47da28f0092bea777fe63e Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Tue, 19 Aug 2025 16:56:24 +0900 Subject: [PATCH] =?UTF-8?q?[20250819]=20BOJ=20/=20G3=20/=20=EB=B2=BD=20?= =?UTF-8?q?=EB=B6=80=EC=88=98=EA=B3=A0=20=EC=9D=B4=EB=8F=99=ED=95=98?= =?UTF-8?q?=EA=B8=B0=202=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 --- ...\353\217\231\355\225\230\352\270\260 2.md" | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 "JHLEE325/202508/19 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" diff --git "a/JHLEE325/202508/19 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" "b/JHLEE325/202508/19 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" new file mode 100644 index 00000000..9523b07f --- /dev/null +++ "b/JHLEE325/202508/19 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" @@ -0,0 +1,99 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int n, m, k; + static int[][] map; + static boolean[][][] visited; + static int[] dr = {-1, 1, 0, 0}; + static int[] dc = {0, 0, -1, 1}; + + static class dot { + int r, c, b; + + public dot(int r, int c, int b) { + this.r = r; + this.c = c; + this.b = b; + } + } + + 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()); + k = Integer.parseInt(st.nextToken()); + + map = new int[n][m]; + visited = new boolean[n][m][k + 1]; + + for (int i = 0; i < n; i++) { + String s = br.readLine(); + for (int j = 0; j < m; j++) { + map[i][j] = Integer.parseInt(String.valueOf(s.charAt(j))); + } + } + if(n==1&&m==1){ + System.out.println(1); + return; + } + + bfs(); + + } + + static void bfs() { + Queue list = new ArrayDeque<>(); + list.add(new dot(0, 0, 0)); + visited[0][0][0] = true; + int cnt = 1; + + while (!list.isEmpty()) { + int s = list.size(); + for (int i = 0; i < s; i++) { + dot cur = list.poll(); + int row = cur.r; + int col = cur.c; + int brk = cur.b; + + for (int d = 0; d < 4; d++) { + int nr = row + dr[d]; + int nc = col + dc[d]; + if (nr < 0 || nr >= n || nc < 0 || nc >= m || visited[nr][nc][brk] || map[nr][nc] == 1) + continue; + visited[nr][nc][brk] = true; + list.add(new dot(nr, nc, brk)); + if (nr == n - 1 && nc == m - 1) { + System.out.println(cnt + 1); + return; + } + } + + if (brk < k) { + for (int d = 0; d < 4; d++) { + int nr = row + dr[d]; + int nc = col + dc[d]; + int nb = brk + 1; + if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue; + if (map[nr][nc] != 1) continue; + if (visited[nr][nc][nb]) continue; + visited[nr][nc][nb] = true; + list.add(new dot(nr, nc, nb)); + if (nr == n - 1 && nc == m - 1) { + System.out.println(cnt + 1); + return; + } + } + } + } + cnt++; + } + System.out.println(-1); + } +} + +```