|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + static int R, C; |
| 20 | + static char[][] arr; |
| 21 | + static char[] command; |
| 22 | + static int x, y; |
| 23 | + static List<int[]> crazy; |
| 24 | + static int[] dx = {0,1,1,1,0,0,0,-1,-1,-1}; |
| 25 | + static int[] dy = {0,-1,0,1,-1,0,1,-1,0,1}; |
| 26 | + |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + |
| 29 | + ready(); |
| 30 | + solve(); |
| 31 | + |
| 32 | + bwEnd(); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + static void ready() throws Exception{ |
| 37 | + |
| 38 | + nextLine(); |
| 39 | + R = nextInt(); |
| 40 | + C = nextInt(); |
| 41 | + arr = new char[R][C]; |
| 42 | + crazy = new ArrayList<>(); |
| 43 | + |
| 44 | + for(int i=0;i<R;i++) { |
| 45 | + char[] temp = br.readLine().toCharArray(); |
| 46 | + for(int j=0;j<C;j++){ |
| 47 | + arr[i][j] = temp[j]; |
| 48 | + if(temp[j] == 'I') { |
| 49 | + x = i; |
| 50 | + y = j; |
| 51 | + } |
| 52 | + if(temp[j] == 'R') crazy.add(new int[]{i,j}); |
| 53 | + } |
| 54 | + } |
| 55 | + command = br.readLine().toCharArray(); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + static void solve() throws Exception{ |
| 60 | + |
| 61 | + int cnt = 0; |
| 62 | + for(char i:command){ |
| 63 | + cnt++; |
| 64 | + if(!move(i-'0')) { |
| 65 | + bw.write("kraj "+cnt+"\n"); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + arr = new char[R][C]; |
| 71 | + for(int i=0;i<R;i++) Arrays.fill(arr[i], '.'); |
| 72 | + arr[x][y] = 'I'; |
| 73 | + for(int[] arduino:crazy) arr[arduino[0]][arduino[1]] = 'R'; |
| 74 | + |
| 75 | + for(int i=0;i<R;i++){ |
| 76 | + for(int j=0;j<C;j++) bw.write(arr[i][j]); |
| 77 | + bw.write("\n"); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + static boolean move(int dir){ |
| 83 | + |
| 84 | + x += dx[dir]; |
| 85 | + y += dy[dir]; |
| 86 | + |
| 87 | + int[][] cnt = new int[R][C]; |
| 88 | + |
| 89 | + for(int[] arduino:crazy){ |
| 90 | + |
| 91 | + if(arduino[0] > x) arduino[0]--; |
| 92 | + else if(arduino[0] < x) arduino[0]++; |
| 93 | + |
| 94 | + if(arduino[1] > y) arduino[1]--; |
| 95 | + else if(arduino[1] < y) arduino[1]++; |
| 96 | + |
| 97 | + cnt[arduino[0]][arduino[1]]++; |
| 98 | + |
| 99 | + if(arduino[0] == x && arduino[1] == y) return false; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + crazy = new ArrayList<>(); |
| 104 | + |
| 105 | + for(int i=0;i<R;i++) for(int j=0;j<C;j++){ |
| 106 | + if(cnt[i][j] == 1) crazy.add(new int[]{i,j}); |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +``` |
0 commit comments