1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+ // (0,1), (1,0), (0, -1), (-1,0)
4+ int dy[4 ] = {0 ,1 ,0 ,-1 };
5+ int dx[4 ] = {1 ,0 ,-1 ,0 };
6+ int N;
7+ int num;
8+ vector<pair<int , int >> apple;
9+ vector<pair<int , char >> direction_change;
10+ deque<pair<int , int >> snake;
11+ int main () {
12+
13+ ios_base::sync_with_stdio (false );
14+ cin.tie (nullptr );
15+ cout.tie (nullptr );
16+
17+ cin >> N;
18+
19+ cin >> num;
20+ for (int i=0 ; i<num; i++) {
21+ int y,x;
22+ cin >> y >> x;
23+ apple.push_back ({y-1 ,x-1 });
24+ }
25+
26+ cin >> num;
27+ for (int i=0 ; i<num; i++) {
28+ int t;
29+ char direction;
30+ cin >> t >> direction;
31+ direction_change.push_back ({t, direction});
32+ }
33+
34+ int t=0 ;
35+ int d_index=0 ;
36+ snake.push_front ({0 ,0 });
37+ bool break_flag = false ;
38+ bool eat_apple = false ;
39+ while (true ) {
40+ t++;
41+ auto [y,x] = snake.front ();
42+ int ny = y + dy[d_index];
43+ int nx = x + dx[d_index];
44+
45+ if (ny < 0 || nx < 0 || ny > N-1 || nx > N-1 ) {
46+ break_flag = true ;
47+ }
48+ for (auto [ty, tx] : snake) {
49+ if (ty == ny && tx == nx){
50+ break_flag = true ;
51+ }
52+ }
53+ if (break_flag) break ;
54+
55+ for (auto &[ay, ax] : apple) {
56+ if (ay == ny && ax == nx) {
57+ eat_apple = true ;
58+ ay = -1 ;
59+ ax = -1 ;
60+ break ;
61+ }
62+ }
63+
64+ snake.push_front ({ny,nx});
65+ if (!eat_apple) {
66+ snake.pop_back ();
67+ } else {
68+ eat_apple = false ;
69+ }
70+
71+ for (auto [time,direct] : direction_change) {
72+ if (time == t) {
73+ if (direct == ' L' ) {
74+ d_index = (d_index + 3 ) % 4 ;
75+ }
76+ if (direct == ' D' ) {
77+ d_index = (d_index + 1 ) % 4 ;
78+ }
79+ }
80+ }
81+
82+ }
83+
84+ cout << t;
85+ return 0 ;
86+ }
0 commit comments