From 3f0c9c8dbcd23f957552e5e6656e6af3aa936e2a Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 9 Jul 2025 20:24:13 +0900 Subject: [PATCH] =?UTF-8?q?[20250709]=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=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\353\217\231\355\225\230\352\270\260.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "lkhyun/202507/9 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.md" diff --git "a/lkhyun/202507/9 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.md" "b/lkhyun/202507/9 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.md" new file mode 100644 index 00000000..8daca1aa --- /dev/null +++ "b/lkhyun/202507/9 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.md" @@ -0,0 +1,70 @@ +```java +import java.util.*; +import java.io.*; +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N,M,K; + static char[][] matrix; + static int[] di = {-1,1,0,0}; + static int[] dj = {0,0,-1,1}; + static int min = Integer.MAX_VALUE; + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + matrix = new char[N][M]; + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + matrix[i][j] = line.charAt(j); + } + } + + BFS(); + if(min == Integer.MAX_VALUE){ + bw.write("-1"); + }else{ + bw.write(min+""); + } + bw.close(); + } + static void BFS() { + Queue q = new LinkedList<>(); + boolean[][][] visited = new boolean[N][M][K+1]; + q.offer(new int[]{0,0,K,1});//i,j,남은 벽 뚫 개수,현재 거리 + visited[0][0][K] = true; + + while (!q.isEmpty()) { + int[] cur = q.poll(); + if(cur[0] == N-1 && cur[1] == M-1){ + min = Math.min(min, cur[3]); + } + + for (int k = 0; k < 4; k++) { + int ni = cur[0] + di[k]; + int nj = cur[1] + dj[k]; + if(ni<0 || ni>=N || nj<0 || nj>=M){ + continue; + } + if(matrix[ni][nj]=='0'){ + if(!visited[ni][nj][cur[2]]){ + q.offer(new int[]{ni,nj,cur[2],cur[3]+1}); + visited[ni][nj][cur[2]] = true; + } + }else if(matrix[ni][nj]=='1'){ + if(cur[2]>0 && !visited[ni][nj][cur[2]-1]){ + q.offer(new int[]{ni,nj,cur[2]-1,cur[3]+1}); + visited[ni][nj][cur[2]-1] = true; + } + } + } + } + } +} + +```