|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class state{ |
| 7 | + int[] red; |
| 8 | + int[] blue; |
| 9 | + int moveCnt; |
| 10 | + state(int redi, int redj, int bluei, int bluej, int moveCnt){ |
| 11 | + this.red = new int[]{redi,redj}; |
| 12 | + this.blue = new int[]{bluei,bluej}; |
| 13 | + this.moveCnt = moveCnt; |
| 14 | + } |
| 15 | + } |
| 16 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 18 | + static StringTokenizer st; |
| 19 | + static int N,M; |
| 20 | + static char[][] board; |
| 21 | + static int[] di = {-1,1,0,0}; |
| 22 | + static int[] dj = {0,0,-1,1}; |
| 23 | + static int[] red = new int[2]; |
| 24 | + static int[] blue = new int[2]; |
| 25 | + static int[] out = new int[2]; |
| 26 | + |
| 27 | + public static void main(String[] args) throws IOException { |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + N = Integer.parseInt(st.nextToken()); |
| 30 | + M = Integer.parseInt(st.nextToken()); |
| 31 | + board = new char[N][M]; |
| 32 | + for (int i = 0; i < N; i++) { |
| 33 | + String line = br.readLine(); |
| 34 | + for (int j = 0; j < M; j++) { |
| 35 | + board[i][j] = line.charAt(j); |
| 36 | + if(board[i][j] == 'O'){ |
| 37 | + out[0] = i; |
| 38 | + out[1] = j; |
| 39 | + }else if(board[i][j] == 'B'){ |
| 40 | + blue[0] = i; |
| 41 | + blue[1] = j; |
| 42 | + }else if(board[i][j] == 'R'){ |
| 43 | + red[0] = i; |
| 44 | + red[1] = j; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + bw.write(BFS() + ""); |
| 49 | + bw.close(); |
| 50 | + } |
| 51 | + |
| 52 | + static int[] moveMarble(int i, int j, int dir) { |
| 53 | + int ni = i; |
| 54 | + int nj = j; |
| 55 | + int moveCnt = 0; |
| 56 | + int reachedHole = 0; |
| 57 | + |
| 58 | + while(true) { |
| 59 | + ni += di[dir]; |
| 60 | + nj += dj[dir]; |
| 61 | + moveCnt++; |
| 62 | + |
| 63 | + if(ni < 0 || ni >= N || nj < 0 || nj >= M || board[ni][nj] == '#') { |
| 64 | + ni -= di[dir]; |
| 65 | + nj -= dj[dir]; |
| 66 | + moveCnt--; |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + if(board[ni][nj] == 'O') { |
| 71 | + reachedHole = 1; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return new int[]{ni, nj, moveCnt, reachedHole}; |
| 77 | + } |
| 78 | + |
| 79 | + static int BFS(){ |
| 80 | + int answer = -1; |
| 81 | + ArrayDeque<state> q = new ArrayDeque<>(); |
| 82 | + q.add(new state(red[0],red[1],blue[0],blue[1],0)); |
| 83 | + boolean[][][][] visited = new boolean[N][M][N][M]; |
| 84 | + visited[red[0]][red[1]][blue[0]][blue[1]] = true; |
| 85 | + |
| 86 | + loop: while(!q.isEmpty()){ |
| 87 | + state cur = q.poll(); |
| 88 | + if(cur.moveCnt >= 10) continue; |
| 89 | + |
| 90 | + for (int k = 0; k < 4; k++) { |
| 91 | + boolean redOut = false; |
| 92 | + boolean blueOut = false; |
| 93 | + |
| 94 | + int[] redResult = moveMarble(cur.red[0], cur.red[1], k); |
| 95 | + int[] blueResult = moveMarble(cur.blue[0], cur.blue[1], k); |
| 96 | + |
| 97 | + int ri = redResult[0]; |
| 98 | + int rj = redResult[1]; |
| 99 | + int redMoveCnt = redResult[2]; |
| 100 | + boolean redReachedHole = redResult[3] == 1; |
| 101 | + |
| 102 | + int bi = blueResult[0]; |
| 103 | + int bj = blueResult[1]; |
| 104 | + int blueMoveCnt = blueResult[2]; |
| 105 | + boolean blueReachedHole = blueResult[3] == 1; |
| 106 | + |
| 107 | + if(redReachedHole) redOut = true; |
| 108 | + if(blueReachedHole) blueOut = true; |
| 109 | + |
| 110 | + if(blueOut) continue; |
| 111 | + |
| 112 | + if(redOut) { |
| 113 | + answer = cur.moveCnt + 1; |
| 114 | + break loop; |
| 115 | + } |
| 116 | + |
| 117 | + //충돌처리 |
| 118 | + if(ri == bi && rj == bj) { |
| 119 | + if(redMoveCnt > blueMoveCnt) { |
| 120 | + ri -= di[k]; |
| 121 | + rj -= dj[k]; |
| 122 | + } else { |
| 123 | + bi -= di[k]; |
| 124 | + bj -= dj[k]; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if(!visited[ri][rj][bi][bj]){ |
| 129 | + q.add(new state(ri,rj,bi,bj,cur.moveCnt+1)); |
| 130 | + visited[ri][rj][bi][bj] = true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return answer; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
0 commit comments