|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 2개의 사탕 |
| 6 | + */ |
| 7 | + |
| 8 | +public class DH_2개의_사탕 { |
| 9 | + static class Point { |
| 10 | + int r, c; |
| 11 | + public Point(int r, int c) { |
| 12 | + this.r = r; |
| 13 | + this.c = c; |
| 14 | + } |
| 15 | + |
| 16 | + public void update(int r, int c) { |
| 17 | + this.r = r; |
| 18 | + this.c = c; |
| 19 | + } |
| 20 | + } |
| 21 | + static int N, M, result = Integer.MAX_VALUE; |
| 22 | + static char[][] map; |
| 23 | + static int[] dr = {-1, 0, 1, 0}, dc = {0, -1, 0, 1}; |
| 24 | + static Point RED, BLUE; |
| 25 | + |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + System.setIn(new FileInputStream("./input/2개의사탕.txt")); |
| 28 | + |
| 29 | + initInput(); |
| 30 | + |
| 31 | + dfs(RED, BLUE, 0, -1); |
| 32 | + |
| 33 | + System.out.println(result == Integer.MAX_VALUE ? -1 : result); |
| 34 | + } |
| 35 | + static void dfs(Point red, Point blue, int depth, int prevDir) { |
| 36 | + |
| 37 | + // 기울이는 횟수가 10번이 초과하는 경우 그만!! |
| 38 | + if(depth == 10) return; |
| 39 | + |
| 40 | + // 네가지 방향으로 기울여보기 |
| 41 | + for(int d = 0; d < 4; d++) { |
| 42 | + // 이전에 있던 위치를 향해서 못가게 하기 |
| 43 | + if(prevDir == (d + 2) % 4) continue; |
| 44 | + |
| 45 | + // 빨간색, 파란색 사탕 각각 기울였을 때, 도달하는 위치 구하기 |
| 46 | + Point nextRed = getNextPoint(red, d); |
| 47 | + Point nextBlue = getNextPoint(blue, d); |
| 48 | + |
| 49 | + // 파란색 사탕이 빠져나가면 안됨 |
| 50 | + if(map[nextBlue.r][nextBlue.c] == 'O') continue; |
| 51 | + |
| 52 | + // 기울였을 때, 파란색 사탕과 빨간색 사탕이 도달하는 위치가 같으면 |
| 53 | + // 이전 위치를 비교해서 각 사탕이 있을 수 있는 위치 정해주기 |
| 54 | + if(nextRed.r == nextBlue.r && nextRed.c == nextBlue.c) { |
| 55 | + // 상 |
| 56 | + if(d == 0) { |
| 57 | + if(red.r > blue.r) nextRed.r += 1; |
| 58 | + else nextBlue.r += 1; |
| 59 | + } |
| 60 | + |
| 61 | + // 좌 |
| 62 | + if(d == 1) { |
| 63 | + if(red.c > blue.c) nextRed.c += 1; |
| 64 | + else nextBlue.c += 1; |
| 65 | + } |
| 66 | + |
| 67 | + // 하 |
| 68 | + if(d == 2) { |
| 69 | + if(red.r < blue.r) nextRed.r -= 1; |
| 70 | + else nextBlue.r -= 1; |
| 71 | + } |
| 72 | + |
| 73 | + // 우 |
| 74 | + if(d == 3) { |
| 75 | + if(red.c < blue.c) nextRed.c -= 1; |
| 76 | + else nextBlue.c -= 1; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // 두 사탕 모두 움직이지 못하는 경우 그만 |
| 81 | + if(red.r == nextRed.r && red.c == nextRed.c && blue.r == nextBlue.r && blue.c == nextBlue.c) continue; |
| 82 | + if(map[nextRed.r][nextRed.c] == 'O' && map[nextBlue.r][nextBlue.c] == 'O') return; |
| 83 | + |
| 84 | + // 빨간색 사탕을 움직일 수 있는 경우 |
| 85 | + if(map[nextRed.r][nextRed.c] == 'O') { |
| 86 | + result = Math.min(result, depth + 1); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + dfs(nextRed, nextBlue, depth + 1, d); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + // 기울였을 때, 사탕이 도달하는 위치 구하기 |
| 96 | + static Point getNextPoint(Point p, int dir) { |
| 97 | + Point next = new Point(p.r, p.c); |
| 98 | + |
| 99 | + while(true) { |
| 100 | + int nr = next.r + dr[dir]; |
| 101 | + int nc = next.c + dc[dir]; |
| 102 | + |
| 103 | + if(!isValid(nr, nc) || map[nr][nc] == '#') break; |
| 104 | + next.update(nr, nc); |
| 105 | + |
| 106 | + if(map[next.r][next.c] == 'O') break; |
| 107 | + } |
| 108 | + |
| 109 | + return next; |
| 110 | + } |
| 111 | + |
| 112 | + static boolean isValid(int r, int c) { |
| 113 | + return r >= 0 && r < N && c >= 0 && c < M; |
| 114 | + } |
| 115 | + |
| 116 | + static void initInput() throws Exception { |
| 117 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 118 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 119 | + |
| 120 | + N = Integer.parseInt(st.nextToken()); |
| 121 | + M = Integer.parseInt(st.nextToken()); |
| 122 | + |
| 123 | + map = new char[N][M]; |
| 124 | + |
| 125 | + for(int r = 0; r < map.length; r++) { |
| 126 | + map[r] = br.readLine().toCharArray(); |
| 127 | + |
| 128 | + for(int c = 0; c < map[r].length; c++) { |
| 129 | + if(map[r][c] != 'R' && map[r][c] != 'B') continue; |
| 130 | + if(map[r][c] == 'R') RED = new Point(r, c); |
| 131 | + if(map[r][c] == 'B') BLUE = new Point(r, c); |
| 132 | + map[r][c] = '.'; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments