From a7215a44d52fc189251de3105199706dfd7762da Mon Sep 17 00:00:00 2001 From: mong3125 Date: Mon, 11 Mar 2024 00:40:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?BOJ2178=5F=EB=AF=B8=EB=A1=9C=ED=83=90?= =?UTF-8?q?=EC=83=89=ED=95=98=EA=B8=B0=20java=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\203\211\355\225\230\352\270\260.java" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" diff --git "a/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" "b/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" new file mode 100644 index 0000000..000cc93 --- /dev/null +++ "b/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" @@ -0,0 +1,86 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class BOJ2178_미로탐색하기 { + + static int n; + static int m; + + static boolean[][] field; + static boolean[][] visited; + 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()); // 가로 크기 + + // 미로 선언 + field = new boolean[n][m]; + visited = new boolean[n][m]; + + // 미로 입력 + for (int i = 0; i < n; i++) { + String line = br.readLine(); + for (int j = 0; j < m; j++) { + field[i][j] = line.charAt(j) == '1'; + } + } + + // 문제 해결 + int answer = bfs(0, 0); + + // 출력 + System.out.println(answer); + } + + public static int bfs(int i, int j) { + Queue queue = new LinkedList<>(); + queue.add(new int[]{i, j, 1}); + visited[i][j] = true; + + while (!queue.isEmpty()) { + int[] now = queue.poll(); + + if (now[0] == n - 1 && now[1] == m - 1) { + return now[2]; + } + + if (isInRange(now[0] - 1, now[1])) { + if (!visited[now[0] - 1][now[1]] && field[now[0] - 1][now[1]]) { + queue.add(new int[]{now[0] - 1, now[1], now[2] + 1}); + visited[now[0] - 1][now[1]] = true; + } + } + if (isInRange(now[0], now[1] - 1)) { + if (!visited[now[0]][now[1] - 1] && field[now[0]][now[1] - 1]) { + queue.add(new int[]{now[0], now[1] - 1, now[2] + 1}); + visited[now[0]][now[1] - 1] = true; + } + } + if (isInRange(now[0] + 1, now[1])) { + if (!visited[now[0] + 1][now[1]] && field[now[0] + 1][now[1]]) { + queue.add(new int[]{now[0] + 1, now[1], now[2] + 1}); + visited[now[0] + 1][now[1]] = true; + } + } + if (isInRange(now[0], now[1] + 1)) { + if (!visited[now[0]][now[1] + 1] && field[now[0]][now[1] + 1]) { + queue.add(new int[]{now[0], now[1] + 1, now[2] + 1}); + visited[now[0]][now[1] + 1] = true; + } + } + } + + return 0; + } + + public static boolean isInRange(int i, int j) { + return 0 <= i && i < n && 0 <= j && j < m; + } +} From 22693d2ed8059ac99b2415e179adc47d38a0951e Mon Sep 17 00:00:00 2001 From: mong3125 Date: Mon, 11 Mar 2024 01:06:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?BOJ2178=5F=EB=AF=B8=EB=A1=9C=ED=83=90?= =?UTF-8?q?=EC=83=89=ED=95=98=EA=B8=B0=20java=20solution=20ver2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\203\211\355\225\230\352\270\260.java" | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git "a/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" "b/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" index 000cc93..f556826 100644 --- "a/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" +++ "b/mong3125/BFS/BOJ2178_\353\257\270\353\241\234\355\203\220\354\203\211\355\225\230\352\270\260.java" @@ -12,6 +12,9 @@ public class BOJ2178_미로탐색하기 { static boolean[][] field; static boolean[][] visited; + + static int[] dx = {1, -1, 0, 0}; + static int[] dy = {0, 0, 1, -1}; public static void main(String[] args) throws IOException { // 입력 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -51,28 +54,15 @@ public static int bfs(int i, int j) { return now[2]; } - if (isInRange(now[0] - 1, now[1])) { - if (!visited[now[0] - 1][now[1]] && field[now[0] - 1][now[1]]) { - queue.add(new int[]{now[0] - 1, now[1], now[2] + 1}); - visited[now[0] - 1][now[1]] = true; - } - } - if (isInRange(now[0], now[1] - 1)) { - if (!visited[now[0]][now[1] - 1] && field[now[0]][now[1] - 1]) { - queue.add(new int[]{now[0], now[1] - 1, now[2] + 1}); - visited[now[0]][now[1] - 1] = true; - } - } - if (isInRange(now[0] + 1, now[1])) { - if (!visited[now[0] + 1][now[1]] && field[now[0] + 1][now[1]]) { - queue.add(new int[]{now[0] + 1, now[1], now[2] + 1}); - visited[now[0] + 1][now[1]] = true; - } - } - if (isInRange(now[0], now[1] + 1)) { - if (!visited[now[0]][now[1] + 1] && field[now[0]][now[1] + 1]) { - queue.add(new int[]{now[0], now[1] + 1, now[2] + 1}); - visited[now[0]][now[1] + 1] = true; + for (int k = 0; k < 4; k++) { + int x = now[0] + dx[k]; + int y = now[1] + dy[k]; + + if (isInRange(x, y)) { + if (!visited[x][y] && field[x][y]) { + queue.add(new int[]{x, y, now[2] + 1}); + visited[x][y] = true; + } } } }