1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ private static final int [] dr = {0 , -1 , 0 , 1 };
7
+ private static final int [] dc = {1 , 0 , -1 , 0 };
8
+
9
+ public static void main (String [] args ) throws IOException {
10
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11
+
12
+ int N = Integer .parseInt (br .readLine ());
13
+ int [][] board = new int [N ][N ];
14
+
15
+ int K = Integer .parseInt (br .readLine ());
16
+ for (int i = 0 ; i < K ; i ++) {
17
+ StringTokenizer st = new StringTokenizer (br .readLine ());
18
+ int r = Integer .parseInt (st .nextToken ()) - 1 ;
19
+ int c = Integer .parseInt (st .nextToken ()) - 1 ;
20
+ board [r ][c ] = 1 ;
21
+ }
22
+
23
+ int L = Integer .parseInt (br .readLine ());
24
+ int direction = 0 ; //오른쪽
25
+ Deque <int []> snake = new ArrayDeque <>();
26
+ snake .addFirst (new int []{0 , 0 });
27
+ board [0 ][0 ] = 2 ;
28
+
29
+ int answer = 0 ;
30
+ for (int i = 0 ; i < L ; i ++){
31
+ StringTokenizer st = new StringTokenizer (br .readLine ());
32
+ int X = Integer .parseInt (st .nextToken ());
33
+ char C = st .nextToken ().charAt (0 );
34
+
35
+ while (answer < X ){
36
+ answer ++;
37
+ int [] current = snake .getFirst ();
38
+ int [] next = new int [2 ];
39
+ next [0 ] = current [0 ] + dr [direction ];
40
+ next [1 ] = current [1 ] + dc [direction ];
41
+
42
+ if (isWall (board , next [0 ], next [1 ]) || isBody (board , next [0 ], next [1 ])){
43
+ System .out .println (answer );
44
+ return ;
45
+ }
46
+
47
+ //다음 머리의 좌표를 추가
48
+ snake .addFirst (next );
49
+
50
+ if (!isApple (board ,next [0 ], next [1 ])){
51
+ int [] tail = snake .getLast ();
52
+ board [tail [0 ]][tail [1 ]] = 0 ;
53
+ snake .removeLast ();
54
+ }
55
+
56
+ board [next [0 ]][next [1 ]] = 2 ;
57
+ }
58
+ direction = changeDirection (direction , C );
59
+ }
60
+
61
+ answer ++;
62
+ int [] current = snake .getFirst ();
63
+ int [] next = new int [2 ];
64
+ next [0 ] = current [0 ] + dr [direction ];
65
+ next [1 ] = current [1 ] + dc [direction ];
66
+
67
+ while (!isWall (board , next [0 ], next [1 ]) && !isBody (board , next [0 ], next [1 ])){
68
+ snake .addFirst (next );
69
+ if (isApple (board ,next [0 ], next [1 ])){
70
+ board [next [0 ]][next [1 ]] = 2 ;
71
+ } else {
72
+ int [] tail = snake .getLast ();
73
+ board [tail [0 ]][tail [1 ]] = 0 ;
74
+ snake .removeLast ();
75
+ }
76
+ answer ++;
77
+ current = snake .getFirst ();
78
+ next = new int [2 ];
79
+ next [0 ] = current [0 ] + dr [direction ];
80
+ next [1 ] = current [1 ] + dc [direction ];
81
+ }
82
+
83
+ System .out .println (answer );
84
+ }
85
+
86
+ private static int changeDirection (int d , char C ){
87
+ if (C == 'L' ){
88
+ return (d + 1 ) % 4 ;
89
+ }else {
90
+ return (d - 1 ) < 0 ? 3 : d - 1 ;
91
+ }
92
+ }
93
+
94
+ private static boolean isApple (int [][] board , int r , int c ){
95
+ return board [r ][c ] == 1 ;
96
+ }
97
+ private static boolean isWall (int [][] board , int r , int c ){
98
+ return (r < 0 || r >= board .length ) || (c < 0 || c >= board .length );
99
+ }
100
+
101
+ private static boolean isBody (int [][] board , int r , int c ){
102
+ return board [r ][c ] == 2 ;
103
+ }
104
+ }
0 commit comments