1
+ import java .util .*;
2
+
3
+ class Node {
4
+
5
+ private int time ;
6
+ private char direction ;
7
+
8
+ public Node (int time , char direction ) {
9
+ this .time = time ;
10
+ this .direction = direction ;
11
+ }
12
+
13
+ public int getTime () {
14
+ return this .time ;
15
+ }
16
+
17
+ public char getDirection () {
18
+ return this .direction ;
19
+ }
20
+ }
21
+
22
+ class Position {
23
+
24
+ private int x ;
25
+ private int y ;
26
+
27
+ public Position (int x , int y ) {
28
+ this .x = x ;
29
+ this .y = y ;
30
+ }
31
+
32
+ public int getX () {
33
+ return this .x ;
34
+ }
35
+
36
+ public int getY () {
37
+ return this .y ;
38
+ }
39
+ }
40
+
41
+ public class Main {
42
+
43
+ public static int n , k , l ;
44
+ public static int [][] arr = new int [101 ][101 ]; // 맵 정보
45
+ public static ArrayList <Node > info = new ArrayList <>(); // 방향 회전 정보
46
+
47
+ // 처음에는 오른쪽을 보고 있으므로(동, 남, 서, 북)
48
+ public static int dx [] = {0 , 1 , 0 , -1 };
49
+ public static int dy [] = {1 , 0 , -1 , 0 };
50
+
51
+ public static int turn (int direction , char c ) {
52
+ if (c == 'L' ) direction = (direction == 0 )? 3 : direction - 1 ;
53
+ else direction = (direction + 1 ) % 4 ;
54
+ return direction ;
55
+ }
56
+
57
+ public static int simulate () {
58
+ int x = 1 , y = 1 ; // 뱀의 머리 위치
59
+ arr [x ][y ] = 2 ; // 뱀이 존재하는 위치는 2로 표시
60
+ int direction = 0 ; // 처음에는 동쪽을 보고 있음
61
+ int time = 0 ; // 시작한 뒤에 지난 '초' 시간
62
+ int index = 0 ; // 다음에 회전할 정보
63
+ // 뱀이 차지하고 있는 위치 정보(꼬리가 앞쪽)
64
+ Queue <Position > q = new LinkedList <>();
65
+ q .offer (new Position (x , y ));
66
+
67
+ while (true ) {
68
+ int nx = x + dx [direction ];
69
+ int ny = y + dy [direction ];
70
+ // 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
71
+ if (1 <= nx && nx <= n && 1 <= ny && ny <= n && arr [nx ][ny ] != 2 ) {
72
+ // 사과가 없다면 이동 후에 꼬리 제거
73
+ if (arr [nx ][ny ] == 0 ) {
74
+ arr [nx ][ny ] = 2 ;
75
+ q .offer (new Position (nx , ny ));
76
+ Position prev = q .poll ();
77
+ arr [prev .getX ()][prev .getY ()] = 0 ;
78
+ }
79
+ // 사과가 있다면 이동 후에 꼬리 그대로 두기
80
+ if (arr [nx ][ny ] == 1 ) {
81
+ arr [nx ][ny ] = 2 ;
82
+ q .offer (new Position (nx , ny ));
83
+ }
84
+ }
85
+ // 벽이나 뱀의 몸통과 부딪혔다면
86
+ else {
87
+ time += 1 ;
88
+ break ;
89
+ }
90
+ // 다음 위치로 머리를 이동
91
+ x = nx ;
92
+ y = ny ;
93
+ time += 1 ;
94
+ if (index < l && time == info .get (index ).getTime ()) { // 회전할 시간인 경우 회전
95
+ direction = turn (direction , info .get (index ).getDirection ());
96
+ index += 1 ;
97
+ }
98
+ }
99
+ return time ;
100
+ }
101
+
102
+ public static void main (String [] args ) {
103
+ Scanner sc = new Scanner (System .in );
104
+
105
+ n = sc .nextInt ();
106
+ k = sc .nextInt ();
107
+
108
+ // 맵 정보(사과 있는 곳은 1로 표시)
109
+ for (int i = 0 ; i < k ; i ++) {
110
+ int a = sc .nextInt ();
111
+ int b = sc .nextInt ();
112
+ arr [a ][b ] = 1 ;
113
+ }
114
+
115
+ // 방향 회전 정보 입력
116
+ l = sc .nextInt ();
117
+ for (int i = 0 ; i < l ; i ++) {
118
+ int x = sc .nextInt ();
119
+ char c = sc .next ().charAt (0 );
120
+ info .add (new Node (x , c ));
121
+ }
122
+
123
+ System .out .println (simulate ());
124
+ }
125
+
126
+ }
0 commit comments