From d60e5dd0e6e2945dccfa9de6108b38f7847564ec Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 13 Nov 2025 09:07:20 +0900 Subject: [PATCH] =?UTF-8?q?[20251113]=20BOJ=20/=20G1=20/=20=EB=8B=AC?= =?UTF-8?q?=EC=9D=B4=20=EC=B0=A8=EC=98=A4=EB=A5=B8=EB=8B=A4,=20=EA=B0=80?= =?UTF-8?q?=EC=9E=90=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\213\244, \352\260\200\354\236\220.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "JHLEE325/202511/13 BOJ G1 \353\213\254\354\235\264 \354\260\250\354\230\244\353\245\270\353\213\244, \352\260\200\354\236\220.md" diff --git "a/JHLEE325/202511/13 BOJ G1 \353\213\254\354\235\264 \354\260\250\354\230\244\353\245\270\353\213\244, \352\260\200\354\236\220.md" "b/JHLEE325/202511/13 BOJ G1 \353\213\254\354\235\264 \354\260\250\354\230\244\353\245\270\353\213\244, \352\260\200\354\236\220.md" new file mode 100644 index 00000000..ba286ece --- /dev/null +++ "b/JHLEE325/202511/13 BOJ G1 \353\213\254\354\235\264 \354\260\250\354\230\244\353\245\270\353\213\244, \352\260\200\354\236\220.md" @@ -0,0 +1,87 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static char[][] map; + static boolean[][][] visited; + static int[] dy = {-1, 1, 0, 0}; + static int[] dx = {0, 0, -1, 1}; + + static class State { + int y, x, keybit, dist; + State(int y, int x, int keybit, int dist) { + this.y = y; + this.x = x; + this.keybit = keybit; + this.dist = dist; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new char[N][M]; + visited = new boolean[1<<6][N][M]; + int starty = 0; + int startx = 0; + + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + map[i][j] = line.charAt(j); + if (map[i][j] == '0') { + starty = i; startx = j; + map[i][j] = '.'; + } + } + } + + int ans = bfs(starty, startx); + System.out.println(ans); + } + + static int bfs(int y, int x) { + Queue q = new LinkedList<>(); + q.offer(new State(y, x, 0, 0)); + visited[0][y][x] = true; + + while (!q.isEmpty()) { + State cur = q.poll(); + if (map[cur.y][cur.x] == '1') { + return cur.dist; + } + + for (int d = 0; d < 4; d++) { + int ny = cur.y + dy[d]; + int nx = cur.x + dx[d]; + int nKey = cur.keybit; + + if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue; + char ch = map[ny][nx]; + + if (ch == '#') continue; + + if (ch >= 'A' && ch <= 'F') { + int needed = 1 << (ch - 'A'); + if ((nKey & needed) == 0) continue; + } + if (ch >= 'a' && ch <= 'f') { + nKey = nKey | (1 << (ch - 'a')); + } + + if (!visited[nKey][ny][nx]) { + visited[nKey][ny][nx] = true; + q.offer(new State(ny, nx, nKey, cur.dist + 1)); + } + } + } + + return -1; + } +} +```