|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class 뱀_고진석 { |
| 5 | + static int[][] map; |
| 6 | + static int N, K, L; |
| 7 | + static int dir; |
| 8 | + static int[] dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0}; |
| 9 | + static boolean finish = false; |
| 10 | + |
| 11 | + public static void main(String[] args) throws Exception { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + StringTokenizer st; |
| 15 | + |
| 16 | + N = Integer.parseInt(br.readLine()); |
| 17 | + K = Integer.parseInt(br.readLine()); |
| 18 | + |
| 19 | + map = new int[N + 2][N + 2]; |
| 20 | + |
| 21 | + Arrays.fill(map[0], -1); |
| 22 | + for(int i = 1 ; i <= N ; i++) { |
| 23 | + map[i][0] = -1; |
| 24 | + map[i][N + 1] = -1; |
| 25 | + } |
| 26 | + Arrays.fill(map[N + 1], -1); |
| 27 | + |
| 28 | + for(int i = 0 ; i < K ; i++) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + map[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 2; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + L = Integer.parseInt(br.readLine()); |
| 35 | + int[] cmd0 = new int[L]; |
| 36 | + char[] cmd1 = new char[L]; |
| 37 | + for(int i = 0 ; i < L ; i++) { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + cmd0[i] = Integer.parseInt(st.nextToken()); |
| 40 | + cmd1[i] = st.nextToken().toCharArray()[0]; |
| 41 | + } |
| 42 | + |
| 43 | + Deque<Point> dq = new ArrayDeque<>(); |
| 44 | + dq.add(new Point(1, 1)); |
| 45 | + map[1][1] = 1; |
| 46 | + dir = 0; |
| 47 | + int sec = 0; |
| 48 | + for(int i = 0 ; i < L ; i++) { |
| 49 | + while(true) { |
| 50 | + sec++; |
| 51 | + int nx = dq.peekFirst().x + dx[dir]; |
| 52 | + int ny = dq.peekFirst().y + dy[dir]; |
| 53 | + if(map[nx][ny] == 1 || map[nx][ny] == -1) { |
| 54 | + finish = true; |
| 55 | + break; |
| 56 | + } |
| 57 | + else if(map[nx][ny] == 2) { |
| 58 | + map[nx][ny] = 1; |
| 59 | + dq.offerFirst(new Point(nx, ny)); |
| 60 | + } |
| 61 | + else { |
| 62 | + map[nx][ny] = 1; |
| 63 | + dq.offerFirst(new Point(nx, ny)); |
| 64 | + Point tail = dq.pollLast(); |
| 65 | + map[tail.x][tail.y] = 0; |
| 66 | + } |
| 67 | + if(sec == cmd0[i]) { |
| 68 | + if(cmd1[i] == 'L') |
| 69 | + dir = (dir + 3) % 4; |
| 70 | + else |
| 71 | + dir = (dir + 1) % 4; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + if(finish) |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + while(!finish) { |
| 80 | + sec++; |
| 81 | + int nx = dq.peekFirst().x + dx[dir]; |
| 82 | + int ny = dq.peekFirst().y + dy[dir]; |
| 83 | + if(map[nx][ny] == 1 || map[nx][ny] == -1) |
| 84 | + break; |
| 85 | + else if(map[nx][ny] == 2) { |
| 86 | + map[nx][ny] = 1; |
| 87 | + dq.offerFirst(new Point(nx, ny)); |
| 88 | + } |
| 89 | + else { |
| 90 | + map[nx][ny] = 1; |
| 91 | + dq.offerFirst(new Point(nx, ny)); |
| 92 | + Point tail = dq.pollLast(); |
| 93 | + map[tail.x][tail.y] = 0; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + bw.write(sec + "\n"); |
| 98 | + bw.flush(); |
| 99 | + bw.close(); |
| 100 | + br.close(); |
| 101 | + } |
| 102 | + |
| 103 | + static class Point{ |
| 104 | + int x; |
| 105 | + int y; |
| 106 | + |
| 107 | + public Point(int x, int y) { |
| 108 | + this.x = x; |
| 109 | + this.y = y; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments