From 440bd5cf18e6fa9f69ddd169ec51f61e057065e5 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 30 Jul 2025 15:19:36 +0900 Subject: [PATCH] =?UTF-8?q?[20250730]=20BOJ=20/=20G4=20/=20=EC=A3=BC?= =?UTF-8?q?=EB=82=9C=EC=9D=98=20=EB=82=9C=20/=20=EC=9D=B4=EA=B0=95?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\202\234\354\235\230 \353\202\234.md" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "lkhyun/202507/30 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" diff --git "a/lkhyun/202507/30 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" "b/lkhyun/202507/30 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" new file mode 100644 index 00000000..dd5a1385 --- /dev/null +++ "b/lkhyun/202507/30 BOJ G4 \354\243\274\353\202\234\354\235\230 \353\202\234.md" @@ -0,0 +1,81 @@ +```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; + static char[][] room; + static int[] junan = new int[2]; + static int[] friend = new int[2]; + static int[] di = {0,0,-1,1}; + static int[] dj = {-1,1,0,0}; + public static void main(String[] args) throws Exception { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + room = new char[N+1][M+1]; + + st = new StringTokenizer(br.readLine()); + junan[0] = Integer.parseInt(st.nextToken()); + junan[1] = Integer.parseInt(st.nextToken()); + friend[0] = Integer.parseInt(st.nextToken()); + friend[1] = Integer.parseInt(st.nextToken()); + if(junan[0] == friend[0] && junan[1] == friend[1]){ + bw.write("0"); + bw.close(); + return; + } + + for (int i = 1; i <= N; i++) { + String line = br.readLine(); + for (int j = 1; j <= M; j++) { + room[i][j] = line.charAt(j-1); + } + } + + bw.write(BFS()+""); + bw.close(); + } + static int BFS(){ + ArrayDeque q = new ArrayDeque<>(); + boolean[][] visited = new boolean[N+1][M+1]; + q.offer(new int[]{junan[0],junan[1],1}); + visited[junan[0]][junan[1]] = true; + + while(!q.isEmpty()){ + int[] cur = q.poll(); + if(cur[0] == friend[0] && cur[1] == friend[1]){ + return cur[2]; + } + for (int k = 0; k < 4; k++) { + int ni = cur[0] + di[k]; + int nj = cur[1] + dj[k]; + + while (check(ni, nj, visited)){ + if(room[ni][nj] == '1'){ + room[ni][nj] = '0'; + q.offer(new int[]{ni,nj,cur[2]+1}); + visited[ni][nj] = true; + break; + } + q.push(new int[]{ni,nj,cur[2]}); + visited[ni][nj] = true; + ni += di[k]; + nj += dj[k]; + } + } + } + return 0; + } + static boolean check(int ni,int nj,boolean[][] visited){ + if(ni<=0 || ni>N || nj<=0 || nj>M || visited[ni][nj]){ + return false; + }else{ + return true; + } + } +} +```