From 813e51073959f2a663ebf2e88b5d5cd26f891743 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Mon, 8 Sep 2025 22:27:17 +0900 Subject: [PATCH] =?UTF-8?q?[20250908]=20BOJ=20/=20G4=20/=20=EC=95=8C?= =?UTF-8?q?=EA=B3=A0=EC=8A=A4=ED=8C=9F=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 --- ...14\352\263\240\354\212\244\355\214\237.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "lkhyun/202509/08 BOJ G4 \354\225\214\352\263\240\354\212\244\355\214\237.md" diff --git "a/lkhyun/202509/08 BOJ G4 \354\225\214\352\263\240\354\212\244\355\214\237.md" "b/lkhyun/202509/08 BOJ G4 \354\225\214\352\263\240\354\212\244\355\214\237.md" new file mode 100644 index 00000000..0de9bbfc --- /dev/null +++ "b/lkhyun/202509/08 BOJ G4 \354\225\214\352\263\240\354\212\244\355\214\237.md" @@ -0,0 +1,58 @@ +```java +import java.io.*; +import java.util.*; + +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; + static int[] di = {-1,1,0,0}; + static int[] dj = {0,0,-1,1}; + static int[][] matrix; + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + M = Integer.parseInt(st.nextToken()); + N = Integer.parseInt(st.nextToken()); + + matrix = new int[N][M]; + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + matrix[i][j] = Character.getNumericValue(line.charAt(j)); + } + } + bw.write(BFS()+""); + bw.close(); + } + public static int BFS(){ + ArrayDeque deq = new ArrayDeque<>(); + boolean[][] visited = new boolean[N][M]; + deq.offer(new int[]{0,0,0}); + visited[0][0] = true; + + while(!deq.isEmpty()){ + int[] cur = deq.poll(); + + if(cur[0] == N-1 && cur[1] == M-1) return cur[2]; + + for (int i = 0; i < 4; i++) { + int ni = cur[0] + di[i]; + int nj = cur[1] + dj[i]; + + if(ni<0 || ni>=N || nj<0 || nj>=M || visited[ni][nj]) continue; + + if(matrix[ni][nj] == 0){ + deq.offerFirst(new int[]{ni,nj,cur[2]}); + visited[ni][nj] = true; + }else{ + deq.offer(new int[]{ni,nj,cur[2]+1}); + visited[ni][nj] = true; + } + } + } + return 0; + } +} +```