|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Node { |
| 7 | + int x, y, dir, time; |
| 8 | + Node(int x, int y, int dir, int time){ |
| 9 | + this.x = x; |
| 10 | + this.y = y; |
| 11 | + this.dir = dir; |
| 12 | + this.time = time; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class Main { |
| 17 | + |
| 18 | + // IO field |
| 19 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 21 | + static StringTokenizer st; |
| 22 | + |
| 23 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 24 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 25 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 26 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 27 | + |
| 28 | + // Additional field |
| 29 | + static int[] dx = {-1,0,1,0}; |
| 30 | + static int[] dy = {0,1,0,-1}; |
| 31 | + static int H, W, R, C, D; |
| 32 | + |
| 33 | + static int[][] A, B; |
| 34 | + static boolean[][] dust; |
| 35 | + |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + |
| 38 | + ready(); |
| 39 | + solve(); |
| 40 | + |
| 41 | + bwEnd(); |
| 42 | + } |
| 43 | + |
| 44 | + static void ready() throws Exception{ |
| 45 | + |
| 46 | + nextLine(); |
| 47 | + H = nextInt(); |
| 48 | + W = nextInt(); |
| 49 | + nextLine(); |
| 50 | + R = nextInt(); |
| 51 | + C = nextInt(); |
| 52 | + D = nextInt(); |
| 53 | + |
| 54 | + A = new int[H][W]; |
| 55 | + B = new int[H][W]; |
| 56 | + for(int i=0;i<H;i++) { |
| 57 | + char[] arr = br.readLine().toCharArray(); |
| 58 | + for(int j=0;j<W;j++) A[i][j] = arr[j]-'0'; |
| 59 | + } |
| 60 | + for(int i=0;i<H;i++) { |
| 61 | + char[] arr = br.readLine().toCharArray(); |
| 62 | + for(int j=0;j<W;j++) B[i][j] = arr[j]-'0'; |
| 63 | + } |
| 64 | + |
| 65 | + dust = new boolean[H][W]; |
| 66 | + } |
| 67 | + |
| 68 | + static void solve() throws Exception{ |
| 69 | + |
| 70 | + int time = 0, ans = 0; |
| 71 | + while(time <= 100000000) { |
| 72 | + |
| 73 | + boolean wasDust = !dust[R][C]; |
| 74 | + |
| 75 | + int PS = dust[R][C] ? B[R][C] : A[R][C]; |
| 76 | + D = (D + PS) % 4; |
| 77 | + dust[R][C] = true; |
| 78 | + R += dx[D]; |
| 79 | + C += dy[D]; |
| 80 | + time++; |
| 81 | + if(wasDust) ans = time; |
| 82 | + if(out(R,C)) break; |
| 83 | + |
| 84 | + } |
| 85 | + bw.write(ans + "\n"); |
| 86 | + } |
| 87 | + |
| 88 | + static boolean out(int x, int y) { |
| 89 | + return x<0 || x>=H || y<0 || y>=W; |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +``` |
0 commit comments