|
| 1 | +package week03.geun0; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.Queue; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +/** |
| 11 | + * <a href = "https://www.acmicpc.net/problem/1726">백준 1726번 - 로봇</a> |
| 12 | + * @since 2025-07-13 |
| 13 | + * @author 장근영 |
| 14 | + * @apiNote 예상 시간 복잡도 {@code O(NM)} |
| 15 | + */ |
| 16 | +public class BOJ_1726 { |
| 17 | + |
| 18 | + static int n, m; |
| 19 | + static int[][] map; |
| 20 | + static boolean[][][] visit; |
| 21 | + static int[] dx = {0, 0, 1, -1}; //동서남북 |
| 22 | + static int[] dy = {1, -1, 0, 0}; //동서남북 |
| 23 | + |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + |
| 28 | + m = Integer.parseInt(st.nextToken()); //세로 |
| 29 | + n = Integer.parseInt(st.nextToken()); //가로 |
| 30 | + |
| 31 | + map = new int[m][n]; |
| 32 | + visit = new boolean[m][n][4]; //방향(동서남북)을 포함한 3차원 방문 배열 필요 |
| 33 | + |
| 34 | + for (int i = 0; i < m; i++) { |
| 35 | + st = new StringTokenizer(br.readLine()); |
| 36 | + for (int j = 0; j < n; j++) { |
| 37 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + //로봇의 출발 지점과 바라보는 방향 |
| 42 | + st = new StringTokenizer(br.readLine()); |
| 43 | + int sx = Integer.parseInt(st.nextToken()) - 1; |
| 44 | + int sy = Integer.parseInt(st.nextToken()) - 1; |
| 45 | + int sdir = Integer.parseInt(st.nextToken()) - 1; |
| 46 | + |
| 47 | + //로봇의 도착 지점과 바라보는 방향 |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + int ex = Integer.parseInt(st.nextToken()) - 1; |
| 50 | + int ey = Integer.parseInt(st.nextToken()) - 1; |
| 51 | + int edir = Integer.parseInt(st.nextToken()) - 1; |
| 52 | + |
| 53 | + bfs(sx, sy, sdir, ex, ey, edir); |
| 54 | + } |
| 55 | + |
| 56 | + private static void bfs(int sx, int sy, int sdir, int ex, int ey, int edir) { |
| 57 | + Queue<int[]> qu = new ArrayDeque<>(); |
| 58 | + qu.offer(new int[]{sx, sy, sdir, 0}); //{x 좌표, y 좌표, 바라보는 방향, 명령 횟수} |
| 59 | + |
| 60 | + visit[sx][sy][sdir] = true; //시작 위치 방문 처리 |
| 61 | + |
| 62 | + while (!qu.isEmpty()) { |
| 63 | + int[] cur = qu.poll(); |
| 64 | + |
| 65 | + int x = cur[0], y = cur[1]; //좌표 |
| 66 | + int dir = cur[2]; //바라보는 방향 |
| 67 | + int count = cur[3]; //명령 횟수 |
| 68 | + |
| 69 | + //도착 지점 + 바라보는 방향까지 같아야 함 주의 |
| 70 | + if (x == ex && y == ey && dir == edir) { |
| 71 | + System.out.println(count); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + //명령 1 - 현재 향하고 있는 방향으로 1~3칸 만큼 움직인다. |
| 76 | + for (int i = 1; i <= 3; i++) { |
| 77 | + int nx = x + (i * dx[dir]); |
| 78 | + int ny = y + (i * dy[dir]); |
| 79 | + |
| 80 | + //범위를 벗어나는 경우 |
| 81 | + if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue; |
| 82 | + |
| 83 | + //2칸 이상 움직일 때 중간에 갈 수 없는 지점이 있으면 break |
| 84 | + //continue를 해버리면 갈 수 없는 지점을 점프해버리는 문제가 발생한다. |
| 85 | + if (map[nx][ny] == 1) break; |
| 86 | + |
| 87 | + if (!visit[nx][ny][dir]) { |
| 88 | + visit[nx][ny][dir] = true; |
| 89 | + qu.offer(new int[]{nx, ny, dir, count + 1}); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + //명령 2 - 왼쪽 90도 회전 |
| 94 | + int left = turnLeft(dir); |
| 95 | + if (!visit[x][y][left]) { |
| 96 | + visit[x][y][left] = true; |
| 97 | + qu.offer(new int[]{x, y, left, count + 1}); |
| 98 | + } |
| 99 | + |
| 100 | + //명령 2 - 오른쪽 90도 회전 |
| 101 | + int right = turnRight(dir); |
| 102 | + if (!visit[x][y][right]) { |
| 103 | + visit[x][y][right] = true; |
| 104 | + qu.offer(new int[]{x, y, right, count + 1}); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static int turnLeft(int dir) { |
| 110 | + switch (dir) { |
| 111 | + case 0: return 3; |
| 112 | + case 1: return 2; |
| 113 | + case 2: return 0; |
| 114 | + case 3: return 1; |
| 115 | + } |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + private static int turnRight(int dir) { |
| 120 | + switch (dir) { |
| 121 | + case 0: return 2; |
| 122 | + case 1: return 3; |
| 123 | + case 2: return 1; |
| 124 | + case 3: return 0; |
| 125 | + } |
| 126 | + return -1; |
| 127 | + } |
| 128 | +} |
0 commit comments