문제 자체도 직관적이고 구현도 직관적이라 어려워 보이는 부분은 없었다
2차원 행렬에 사과를 2 뱀을 1로 표현하고 뱀의 머리와 꼬리를 따로 저장하며 구현했다
구현할 때 뱀의 각각의 방향을 큐에 저장하면서 뱀의 꼬리를 제어했는데 이 방법이 메모리를 더 적게 사용하기는 하지만 다음 x,y 좌표 자체를 큐에 저장하는 편이 코테에는 더 맞는 방법이라고 생각한다
#include<iostream>
#include<queue>
#define pic pair<int, char>
#define pii pair<int , int>
using namespace std;
int board[105][105];
queue<pic> order;
int n;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
void input() {
int tmp;
int x, y;
char dir;
cin >> n >> tmp;
for (int i = 0; i < tmp; i++) {
cin >> x >> y;
board[x][y] = 2;
}
cin >> tmp;
for (int i = 0; i < tmp; i++) {
cin >> x >> dir;
order.push({ x,dir });
}
}
int solve() {
int d = 0;
int ans = 0;
queue<char> dirs;
pii head = { 1,1 };
pii tail = { 1,1 };
board[1][1] = 1;
dirs.push(d);
int time = order.front().first;
char com = order.front().second;
order.pop();
while (true) {
ans += 1;
int nx = head.first + dx[d];
int ny = head.second + dy[d];
if (nx <= 0 || ny <= 0 || nx > n || ny > n) {
return ans;
}
else if (board[nx][ny] == 1) {
return ans;
}
if (board[nx][ny] == 0) {
board[tail.first][tail.second] = 0;
int tmp = dirs.front();
dirs.pop();
tail.first += dx[tmp];
tail.second += dy[tmp];
}
if (ans == time) {
if (com == 'L') {
d = (d + 3) % 4;
}
else if (com == 'D') {
d = (d + 1) % 4;
}
if (!order.empty()) {
time = order.front().first;
com = order.front().second;
order.pop();
}
}
head.first = nx;
head.second = ny;
dirs.push(d);
board[nx][ny] = 1;
}
}
int main() {
input();
cout << solve();
}