From 5bfde5d4b9ff5018ed7d8cecc3c3b696a6e8d5b7 Mon Sep 17 00:00:00 2001 From: GeunYeong Date: Sun, 13 Jul 2025 20:43:11 +0900 Subject: [PATCH 1/2] BOJ_1726 --- src/week03/geun0/BOJ_1726.java | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/week03/geun0/BOJ_1726.java diff --git a/src/week03/geun0/BOJ_1726.java b/src/week03/geun0/BOJ_1726.java new file mode 100644 index 0000000..52da75a --- /dev/null +++ b/src/week03/geun0/BOJ_1726.java @@ -0,0 +1,128 @@ +package week03.geun0; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +/** + * 백준 1726번 - 로봇 + * @since 2025-07-13 + * @author 장근영 + * @apiNote 예상 시간 복잡도 {@code O(NM)} + */ +public class BOJ_1726 { + + static int n, m; + static int[][] map; + static boolean[][][] visit; + static int[] dx = {0, 0, 1, -1}; //동서남북 + static int[] dy = {1, -1, 0, 0}; //동서남북 + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + m = Integer.parseInt(st.nextToken()); //세로 + n = Integer.parseInt(st.nextToken()); //가로 + + map = new int[m][n]; + visit = new boolean[m][n][4]; //방향(동서남북)을 포함한 3차원 방문 배열 필요 + + for (int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < n; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + } + } + + //로봇의 출발 지점과 바라보는 방향 + st = new StringTokenizer(br.readLine()); + int sx = Integer.parseInt(st.nextToken()) - 1; + int sy = Integer.parseInt(st.nextToken()) - 1; + int sdir = Integer.parseInt(st.nextToken()) - 1; + + //로봇의 도착 지점과 바라보는 방향 + st = new StringTokenizer(br.readLine()); + int ex = Integer.parseInt(st.nextToken()) - 1; + int ey = Integer.parseInt(st.nextToken()) - 1; + int edir = Integer.parseInt(st.nextToken()) - 1; + + bfs(sx, sy, sdir, ex, ey, edir); + } + + private static void bfs(int sx, int sy, int sdir, int ex, int ey, int edir) { + Queue qu = new ArrayDeque<>(); + qu.offer(new int[]{sx, sy, sdir, 0}); //{x 좌표, y 좌표, 바라보는 방향, 명령 횟수} + + visit[sx][sy][sdir] = true; //시작 위치 방문 처리 + + while (!qu.isEmpty()) { + int[] cur = qu.poll(); + + int x = cur[0], y = cur[1]; //좌표 + int dir = cur[2]; //바라보는 방향 + int count = cur[3]; //명령 횟수 + + //도착 지점 + 바라보는 방향까지 같아야 함 주의 + if (x == ex && y == ey && dir == edir) { + System.out.println(count); + return; + } + + //명령 1 - 현재 향하고 있는 방향으로 1~3칸 만큼 움직인다. + for (int i = 1; i <= 3; i++) { + int nx = x + (i * dx[dir]); + int ny = y + (i * dy[dir]); + + //범위를 벗어나는 경우 + if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue; + + //2칸 이상 움직일 때 중간에 갈 수 없는 지점이 있으면 break + //continue를 해버리면 갈 수 없는 지점을 점프해버리는 문제가 발생한다. + if (map[nx][ny] == 1) break; + + if (!visit[nx][ny][dir]) { + visit[nx][ny][dir] = true; + qu.offer(new int[]{nx, ny, dir, count + 1}); + } + } + + //명령 2 - 왼쪽 90도 회전 + int left = turnLeft(dir); + if (!visit[x][y][left]) { + visit[x][y][left] = true; + qu.offer(new int[]{x, y, left, count + 1}); + } + + //명령 2 - 오른쪽 90도 회전 + int right = turnRight(dir); + if (!visit[x][y][right]) { + visit[x][y][right] = true; + qu.offer(new int[]{x, y, right, count + 1}); + } + } + } + + private static int turnLeft(int dir) { + switch (dir) { + case 0: return 3; + case 1: return 2; + case 2: return 0; + case 3: return 1; + } + return -1; + } + + private static int turnRight(int dir) { + switch (dir) { + case 0: return 2; + case 1: return 3; + case 2: return 1; + case 3: return 0; + } + return -1; + } +} From 2eda1f2f14f69fa147576bd09f7648ca28a72520 Mon Sep 17 00:00:00 2001 From: GeunYeong Date: Sun, 13 Jul 2025 21:51:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=B4=87=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20switch-case=20=E2=86=92=20=EB=B0=B0=EC=97=B4=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week03/geun0/BOJ_1726.java | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/week03/geun0/BOJ_1726.java b/src/week03/geun0/BOJ_1726.java index 52da75a..5aa249e 100644 --- a/src/week03/geun0/BOJ_1726.java +++ b/src/week03/geun0/BOJ_1726.java @@ -20,6 +20,8 @@ public class BOJ_1726 { static boolean[][][] visit; static int[] dx = {0, 0, 1, -1}; //동서남북 static int[] dy = {1, -1, 0, 0}; //동서남북 + static int[] turnLeft = {3, 2, 0, 1}; + static int[] turnRight = {2, 3, 1, 0}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -91,38 +93,18 @@ private static void bfs(int sx, int sy, int sdir, int ex, int ey, int edir) { } //명령 2 - 왼쪽 90도 회전 - int left = turnLeft(dir); + int left = turnLeft[dir]; if (!visit[x][y][left]) { visit[x][y][left] = true; qu.offer(new int[]{x, y, left, count + 1}); } //명령 2 - 오른쪽 90도 회전 - int right = turnRight(dir); + int right = turnRight[dir]; if (!visit[x][y][right]) { visit[x][y][right] = true; qu.offer(new int[]{x, y, right, count + 1}); } } } - - private static int turnLeft(int dir) { - switch (dir) { - case 0: return 3; - case 1: return 2; - case 2: return 0; - case 3: return 1; - } - return -1; - } - - private static int turnRight(int dir) { - switch (dir) { - case 0: return 2; - case 1: return 3; - case 2: return 1; - case 3: return 0; - } - return -1; - } }