|
| 1 | +```java |
| 2 | +import java.io.IOException; |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.awt.Point; |
| 5 | +import java.io.*; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | + |
| 9 | +public class Main { |
| 10 | + |
| 11 | + static int height,width,len,nTargetY,nTargetX,res; |
| 12 | + |
| 13 | + static int[] dy = {0,1,1,1,0,0,0,-1,-1,-1}; |
| 14 | + static int[] dx = {0,-1,0,1,-1,0,1,-1,0,1}; |
| 15 | + static int[] dir; |
| 16 | + |
| 17 | + static Point target; |
| 18 | + static Queue<Arduino> arduinos; |
| 19 | + static Arduino[][] visited; |
| 20 | + static StringBuilder sb = new StringBuilder(); |
| 21 | + |
| 22 | + static class Arduino{ |
| 23 | + Point p; |
| 24 | + boolean isOkay; |
| 25 | + |
| 26 | + public Arduino (int x, int y){ |
| 27 | + p = new Point(x,y); |
| 28 | + isOkay = true; |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + init(); |
| 35 | + process(); |
| 36 | + print(); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public static void init() throws IOException { |
| 41 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 42 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 43 | + |
| 44 | + height = Integer.parseInt(st.nextToken()); |
| 45 | + width = Integer.parseInt(st.nextToken()); |
| 46 | + arduinos = new LinkedList<>(); |
| 47 | + |
| 48 | + for (int i = 0; i < height; i++) { |
| 49 | + String[] input = br.readLine().split(""); |
| 50 | + for (int j = 0; j < width; j++) { |
| 51 | + String temp = input[j]; |
| 52 | + |
| 53 | + if (temp.equals("I")) target = new Point(j,i); |
| 54 | + else if (temp.equals("R")) arduinos.add(new Arduino(j,i)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + String[] rawNum = br.readLine().split(""); |
| 59 | + len = rawNum.length; |
| 60 | + dir = new int[len]; |
| 61 | + for (int i = 0; i < len; i++) dir[i] = Integer.parseInt(rawNum[i]); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public static void process() throws IOException { |
| 66 | + |
| 67 | + res = -1; // -1이 유지되면 끝까지 이동한것. |
| 68 | + |
| 69 | + for (int i = 0; i < len; i++) { |
| 70 | + visited = new Arduino [height][width]; |
| 71 | + nTargetY = target.y + dy[dir[i]]; |
| 72 | + nTargetX = target.x + dx[dir[i]]; |
| 73 | + |
| 74 | + target = new Point(nTargetX,nTargetY); |
| 75 | + |
| 76 | + processEach(i+1); |
| 77 | + |
| 78 | + if (res != -1) break; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + makeAns(); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + private static void processEach(int round) { |
| 89 | + int qSize = arduinos.size(); |
| 90 | + |
| 91 | + for (int j = 0 ; j < qSize; j++) { |
| 92 | + Arduino a = arduinos.poll(); |
| 93 | + |
| 94 | + if (!a.isOkay) continue; |
| 95 | + |
| 96 | + int ny = a.p.y; |
| 97 | + int nx = a.p.x; |
| 98 | + |
| 99 | + if (a.p.y != nTargetY) ny += a.p.y < nTargetY?(1):(-1); |
| 100 | + if (a.p.x != nTargetX) nx += a.p.x < nTargetX?(1):(-1); |
| 101 | + |
| 102 | + a.p.y = ny; |
| 103 | + a.p.x = nx; |
| 104 | + |
| 105 | + if (ny == nTargetY && nx == nTargetX) { |
| 106 | + res = round; |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + if (visited[ny][nx] == null) { |
| 111 | + visited[ny][nx] = a; |
| 112 | + arduinos.add(a); |
| 113 | + } else { |
| 114 | + visited[ny][nx].isOkay = false; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void makeAns() { |
| 120 | + if (res != -1) { |
| 121 | + sb.append("kraj ").append(res).append("\n"); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + String[][] ansArr = new String[height][width]; |
| 126 | + |
| 127 | + for (int i = 0; i < height; i++) Arrays.fill(ansArr[i], "."); |
| 128 | + |
| 129 | + ansArr[target.y][target.x] = "I"; |
| 130 | + |
| 131 | + while(!arduinos.isEmpty()) { |
| 132 | + Arduino a = arduinos.poll(); |
| 133 | + if (!a.isOkay) continue; |
| 134 | + |
| 135 | + ansArr[a.p.y][a.p.x] = "R"; |
| 136 | + } |
| 137 | + |
| 138 | + for (int i = 0; i < height; i++) { |
| 139 | + for (int j = 0; j <width; j++) { |
| 140 | + sb.append(ansArr[i][j]); |
| 141 | + } |
| 142 | + sb.append("\n"); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + public static void print() { |
| 148 | + System.out.print(sb.toString()); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +``` |
0 commit comments