|
| 1 | +```java |
| 2 | +import java.awt.*; |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +public class BJ_6087_레이저_통신 { |
| 8 | + |
| 9 | + private static final int[] dx = { -1, 1, 0, 0 }; |
| 10 | + private static final int[] dy = { 0, 0, -1, 1 }; |
| 11 | + |
| 12 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + private static StringTokenizer st; |
| 15 | + |
| 16 | + private static int W, H, ans; |
| 17 | + private static Point start, end; |
| 18 | + private static char[][] matrix; |
| 19 | + private static int[][][] visited; |
| 20 | + private static Queue<Node> pq; |
| 21 | + |
| 22 | + private static class Node implements Comparable<Node> { |
| 23 | + int x; |
| 24 | + int y; |
| 25 | + int dir; |
| 26 | + int cnt; |
| 27 | + |
| 28 | + public Node(int x, int y, int dir, int cnt) { |
| 29 | + this.x = x; |
| 30 | + this.y = y; |
| 31 | + this.dir = dir; |
| 32 | + this.cnt = cnt; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public int compareTo(Node o) { |
| 37 | + return Integer.compare(this.cnt, o.cnt); |
| 38 | + } |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public static void main(String[] args) throws IOException { |
| 43 | + init(); |
| 44 | + sol(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void init() throws IOException { |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + W = Integer.parseInt(st.nextToken()); |
| 50 | + H = Integer.parseInt(st.nextToken()); |
| 51 | + |
| 52 | + visited = new int[H][W][4]; |
| 53 | + for (int i = 0; i < H; i++) { |
| 54 | + for (int j = 0; j < W; j++) { |
| 55 | + Arrays.fill(visited[i][j], Integer.MAX_VALUE); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pq = new PriorityQueue<>(); |
| 60 | + matrix = new char[H][W]; |
| 61 | + for (int i = 0; i < H; i++) { |
| 62 | + String line = br.readLine(); |
| 63 | + for (int j = 0; j < W; j++) { |
| 64 | + matrix[i][j] = line.charAt(j); |
| 65 | + |
| 66 | + if (matrix[i][j] == 'C') { |
| 67 | + if (Objects.isNull(start)) { |
| 68 | + start = new Point(i, j); |
| 69 | + for (int d = 0; d < 4; d++) { |
| 70 | + visited[i][j][d] = 0; |
| 71 | + pq.offer(new Node(i, j, d, 0)); |
| 72 | + } |
| 73 | + } else { |
| 74 | + end = new Point(i, j); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void sol() throws IOException { |
| 82 | + while (!pq.isEmpty()) { |
| 83 | + Node cur = pq.poll(); |
| 84 | + |
| 85 | + if (cur.x == end.x && cur.y == end.y) { |
| 86 | + int ans = Integer.MAX_VALUE; |
| 87 | + for (int d = 0; d < 4; d++) { |
| 88 | + ans = Math.min(ans, visited[end.x][end.y][d]); |
| 89 | + } |
| 90 | + bw.write(ans + ""); |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + if (cur.cnt > visited[cur.x][cur.y][cur.dir]) continue; |
| 95 | + |
| 96 | + for (int d = 0; d < 4; d++) { |
| 97 | + if (d == (cur.dir ^ 1)) continue; |
| 98 | + |
| 99 | + int nx = cur.x + dx[d]; |
| 100 | + int ny = cur.y + dy[d]; |
| 101 | + |
| 102 | + if (OOB(nx, ny) || matrix[nx][ny] == '*') continue; |
| 103 | + |
| 104 | + int nCnt = cur.cnt + (d == cur.dir ? 0 : 1); |
| 105 | + |
| 106 | + if (visited[nx][ny][d] > nCnt) { |
| 107 | + visited[nx][ny][d] = nCnt; |
| 108 | + pq.offer(new Node(nx, ny, d, nCnt)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + bw.flush(); |
| 113 | + bw.close(); |
| 114 | + br.close(); |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean OOB(int x, int y) { |
| 118 | + return x < 0 || H <= x || y < 0 || W <= y; |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | +``` |
0 commit comments