|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class Solution |
| 6 | +{ |
| 7 | + static int N; |
| 8 | + static int M; |
| 9 | + static char[][] board; |
| 10 | + public static void main(String[] args) throws Exception{ |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 13 | + N = Integer.parseInt(st.nextToken()); |
| 14 | + M = Integer.parseInt(st.nextToken()); |
| 15 | + board = new char[N][M]; |
| 16 | + for (int i = 0; i < N; i++) { |
| 17 | + String line = br.readLine(); |
| 18 | + for (int j = 0; j < M; j++) { |
| 19 | + board[i][j] = line.charAt(j); |
| 20 | + } |
| 21 | + } |
| 22 | + st = new StringTokenizer(br.readLine()); |
| 23 | + int PR = Integer.parseInt(st.nextToken()); |
| 24 | + int PC = Integer.parseInt(st.nextToken()); |
| 25 | + |
| 26 | + int[][] dir = new int[][] {{1,0},{-1,0},{0,1},{0,-1}}; //D, U, R, L |
| 27 | + char cdir = 'U'; |
| 28 | + int answer = 0; |
| 29 | + for (int i = 0; i < 4; i++) { |
| 30 | + int[] d = dir[i]; |
| 31 | + int res = check(PR-1, PC-1, d); |
| 32 | + if (res > answer) { |
| 33 | + if (i == 0) cdir = 'D'; |
| 34 | + else if (i == 1) cdir = 'U'; |
| 35 | + else if (i == 2) cdir = 'R'; |
| 36 | + else cdir = 'L'; |
| 37 | + answer = res; |
| 38 | + } |
| 39 | + if (res == Integer.MAX_VALUE) { |
| 40 | + if (i == 1 && (cdir == 'R' || cdir == 'D' || cdir == 'L')) cdir = 'U'; |
| 41 | + else if (i == 2 && (cdir == 'D' || cdir == 'L')) cdir = 'R'; |
| 42 | + else if (i == 0 && cdir == 'L') cdir = 'D'; |
| 43 | + } |
| 44 | + } |
| 45 | + System.out.println(cdir); |
| 46 | + if (answer == Integer.MAX_VALUE) { |
| 47 | + System.out.println("Voyager"); |
| 48 | + } else { |
| 49 | + System.out.println(answer); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static int check(int cy, int cx, int[] d) { |
| 54 | + int cnt = 1; |
| 55 | + while(true) { |
| 56 | + cy += d[0]; |
| 57 | + cx += d[1]; |
| 58 | + if (cnt > N*M*2) return Integer.MAX_VALUE; |
| 59 | + if (cy < 0 || cy >= N || cx < 0 || cx >= M || board[cy][cx] == 'C') break; |
| 60 | + if (board[cy][cx] == '/') { |
| 61 | + int temp = d[0]; |
| 62 | + d[0] = d[1]*-1; |
| 63 | + d[1] = temp*-1; |
| 64 | + }else if (board[cy][cx] == '\\') { |
| 65 | + int temp = d[0]; |
| 66 | + d[0] = d[1]; |
| 67 | + d[1] = temp; |
| 68 | + } |
| 69 | + cnt += 1; |
| 70 | + } |
| 71 | + return cnt; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +``` |
0 commit comments