|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class SB_두개의사탕 { |
| 7 | + static int N, M; |
| 8 | + static char[][] board; |
| 9 | + static int[] blue = new int[2]; |
| 10 | + static int[] red = new int[2]; |
| 11 | + static int[] dx = {-1, 1, 0, 0}; |
| 12 | + static int[] dy = {0, 0, -1, 1}; |
| 13 | + |
| 14 | + private static Res tilt(int x, int y, int i) { // 방향에 따라 기울이기 |
| 15 | + int move = 0; |
| 16 | + while (board[x + dx[i]][y + dy[i]] != '#' && board[x][y] != 'O') { // 다음에 위치할 값이 #이 아니고 현재 값이 도착점이 아니면 계속 움직임 |
| 17 | + x += dx[i]; |
| 18 | + y += dy[i]; // 좌표값 갱신 (i방향으로 이동) |
| 19 | + move++; |
| 20 | + } |
| 21 | + return new Res(x, y, move, board[x][y] == 'O'); |
| 22 | + } |
| 23 | + |
| 24 | + private static int bfs() { |
| 25 | + Deque<Node> que = new ArrayDeque<>(); |
| 26 | + Set<String> visited = new HashSet<>(); |
| 27 | + |
| 28 | + que.offer(new Node(red[0], red[1], blue[0], blue[1], 0)); |
| 29 | + visited.add("" + red[0] + red[1] + blue[0] + blue[1]); |
| 30 | + |
| 31 | + while (!que.isEmpty()) { |
| 32 | + Node cur = que.poll(); |
| 33 | + if (cur.cnt >= 10) return -1; |
| 34 | + |
| 35 | + for (int i = 0; i < 4; i++) { |
| 36 | + // 구슬 각각 기울인 값 |
| 37 | + Res nR = tilt(cur.rx, cur.ry, i); |
| 38 | + Res nB = tilt(cur.bx, cur.by, i); |
| 39 | + |
| 40 | + if (nB.isGoal) continue; |
| 41 | + if (nR.isGoal) return cur.cnt + 1; |
| 42 | + if (nB.x == nR.x && nB.y == nR.y) { // 구슬이 같은 위치에 있다고 나왔을땐, 거리를 통해 실제 위치 파악 |
| 43 | + if (nR.mv > nB.mv){ // 이동거리가 많은 것이 늦게 온것, 따라서 기존 위치보다 한 칸 뒤에 존재 |
| 44 | + nR.x -= dx[i]; |
| 45 | + nR.y -= dy[i]; |
| 46 | + } else{ |
| 47 | + nB.x -= dx[i]; |
| 48 | + nB.y -= dy[i]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + String state = "" + nR.x + nR.y + nB.x + nB.y; |
| 53 | + if (!visited.contains(state)) { |
| 54 | + visited.add(state); |
| 55 | + que.offer(new Node(nR.x, nR.y, nB.x, nB.y, cur.cnt + 1)); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return -1; |
| 60 | + } |
| 61 | + public static void main(String[] args) throws IOException { |
| 62 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 63 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 64 | + |
| 65 | + N = Integer.parseInt(st.nextToken()); |
| 66 | + M = Integer.parseInt(st.nextToken()); |
| 67 | + |
| 68 | + board = new char[N][M]; |
| 69 | + |
| 70 | + for (int i = 0; i < N; i++) { |
| 71 | + String line = br.readLine(); |
| 72 | + for (int j = 0; j < M; j++) { |
| 73 | + board[i][j] = line.charAt(j); |
| 74 | + if (board[i][j]=='B') { |
| 75 | + blue[0] = i; |
| 76 | + blue[1] = j; |
| 77 | + } |
| 78 | + else if (board[i][j]=='R') { |
| 79 | + red[0] = i; |
| 80 | + red[1] = j; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + System.out.println(bfs()); |
| 86 | + } |
| 87 | + |
| 88 | + private static class Node{ |
| 89 | + int rx, ry, bx, by, cnt; |
| 90 | + |
| 91 | + public Node(int rx, int ry, int bx, int by, int cnt) { |
| 92 | + this.rx = rx; |
| 93 | + this.ry = ry; |
| 94 | + this.bx = bx; |
| 95 | + this.by = by; |
| 96 | + this.cnt = cnt; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static class Res{ |
| 101 | + int x, y, mv; |
| 102 | + boolean isGoal; |
| 103 | + |
| 104 | + public Res(int x, int y, int mv, boolean isGoal) { |
| 105 | + this.x = x; |
| 106 | + this.y = y; |
| 107 | + this.mv = mv; |
| 108 | + this.isGoal = isGoal; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments