소스 코드
#include <iostream>
#include <deque>
using namespace std;
int main() {
int n, k;
int map(101)(101);
int l, now = 1;
int direction = 2;
pair<int, int> p = {1, 1};
int dx(4) = {0, -1, 0, 1};
int dy(4) = {-1, 0, 1, 0};
deque< pair<int, int> > snake;
cin >> n;
cin >> k;
for(int i=0; i<k; i++) {
int a, b;
cin >> a >> b;
map(a)(b) = 4;
}
cin >> l;
snake.push_front({1, 1});
for(int i=0; i<=l; i++) {
long long time;
char cmd;
if(i < l) cin >> time >> cmd;
else time = 999999999999999999;
for(now = now; now<=time; now++) {
p.first = p.first + dx(direction);
p.second = p.second + dy(direction);
if(p.first > n || p.second > n || p.first <= 0 || p.second <= 0) {
cout << now << "\n";
return 0;
}
for(int j=0; j<snake.size(); j++) {
if(snake(j).first == p.first && snake(j).second == p.second) {
cout << now << "\n";
return 0;
}
}
if(map(p.first)(p.second) !
= 4) {
snake.pop_back();
}
else {
map(p.first)(p.second) = 0;
}
snake.push_front({p.first, p.second});
}
if(cmd == 'D') {
direction++;
if (direction>3) {
direction = 0;
}
}
else if(cmd == 'L') {
direction--;
if(direction < 0) {
direction = 3;
}
}
}
}