|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class boj13460 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 9 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 10 | + |
| 11 | + static char[][] board; |
| 12 | + static int N, M, answer = 11; |
| 13 | + static Pos R, B, T; |
| 14 | + |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + nextLine(); |
| 17 | + N = nextInt(); |
| 18 | + M = nextInt(); |
| 19 | + board = new char[N][M]; |
| 20 | + |
| 21 | + for (int i = 0; i < N; i++) { |
| 22 | + String line = br.readLine(); |
| 23 | + for (int j = 0; j < M; j++) { |
| 24 | + board[i][j] = line.charAt(j); |
| 25 | + if (board[i][j] == 'R') R = new Pos(i, j, 'I'); |
| 26 | + else if (board[i][j] == 'B') B = new Pos(i, j, 'I'); |
| 27 | + else if (board[i][j] == 'O') T = new Pos(i, j, 'I'); |
| 28 | + } |
| 29 | + } |
| 30 | + move(R, B, 0); |
| 31 | + if (answer == 11) System.out.println(-1); |
| 32 | + else System.out.println(answer); |
| 33 | + } |
| 34 | + |
| 35 | + static void move(Pos cR, Pos cB, int cnt) { |
| 36 | + if (cnt >= 10) return; |
| 37 | + int[][] dir; |
| 38 | + if (cR.m == 'V') { |
| 39 | + dir = new int[][] {{0,-1}, {0,1}}; |
| 40 | + } else if (cR.m == 'H') { |
| 41 | + dir = new int[][] {{-1, 0}, {1,0}}; |
| 42 | + } else { |
| 43 | + dir = new int[][] {{-1, 0}, {1,0}, {0,-1}, {0,1}}; |
| 44 | + } |
| 45 | + for (int[] d : dir) { |
| 46 | + Pos nR = new Pos(cR.y, cR.x, cR.m); |
| 47 | + Pos nB = new Pos(cB.y, cB.x, cB.m); |
| 48 | + boolean RFlag = false, BFlag = false; |
| 49 | + while (checkRange(nR.y+d[0], nR.x+d[1])) { |
| 50 | + if (nR.y+d[0] == nB.y && nR.x+d[1] == nB.x) break; |
| 51 | + nR.y += d[0]; |
| 52 | + nR.x += d[1]; |
| 53 | + if (nR.y == T.y && nR.x == T.x) { |
| 54 | + RFlag = true; |
| 55 | + nR.y = -1; |
| 56 | + nR.x = -1; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + while (checkRange(nB.y+d[0], nB.x+d[1])) { |
| 61 | + if (nB.y+d[0] == nR.y && nB.x+d[1] == nR.x) break; |
| 62 | + nB.y += d[0]; |
| 63 | + nB.x += d[1]; |
| 64 | + if (nB.y == T.y && nB.x == T.x) { |
| 65 | + BFlag = true; |
| 66 | + nB.y = -1; |
| 67 | + nB.x = -1; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + while (checkRange(nR.y+d[0], nR.x+d[1])) { |
| 72 | + if (nR.y+d[0] == nB.y && nR.x+d[1] == nB.x) break; |
| 73 | + nR.y += d[0]; |
| 74 | + nR.x += d[1]; |
| 75 | + if (nR.y == T.y && nR.x == T.x) { |
| 76 | + RFlag = true; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + if (cR.y == nR.y && cR.x == nR.x && cB.y == nB.y && cB.x == nB.x) continue; |
| 81 | + if (nR.m == 'H') { |
| 82 | + nR.m = 'V'; |
| 83 | + nB.m = 'V'; |
| 84 | + } else if (nR.m == 'V') { |
| 85 | + nR.m = 'H'; |
| 86 | + nB.m = 'H'; |
| 87 | + } |
| 88 | + if ((RFlag && BFlag) || (!RFlag && BFlag)) continue; |
| 89 | + else if (RFlag && !BFlag) { |
| 90 | + answer = Math.min(answer, cnt+1); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + move(nR, nB, cnt+1); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static boolean checkRange(int y, int x) { |
| 99 | + if (y > 0 && y < N-1 && x > 0 && x < M-1 && board[y][x] != '#') return true; |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + static class Pos { |
| 104 | + int y, x; |
| 105 | + char m; |
| 106 | + public Pos(int y, int x, char m) { |
| 107 | + this.y = y; |
| 108 | + this.x = x; |
| 109 | + this.m = m; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +``` |
0 commit comments