그냥 자료구조 큐로 단순히 조지는 문제였다.
바로 코드
/**
* @file 3190.cpp
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2023-03-22
*
* @copyright Copyright (c) 2023
*
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,char> pic;
typedef pair<int,int> pii;
// right, down, left, up
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int N,K,L;
int main(){
cin >> N;
cin >> K;
int apple[101][101]={0};
int board[101][101]={0};
int times=0;
int row=1, col=1;
int direction=0;
queue<pic> move;
queue<pii> snake;
snake.push(make_pair(1,1));
board[1][1]=1;
for(int i=0;i<K;i++){
int x,y;
cin >> x >> y;
apple[x][y]=1;
}
cin >> L;
for(int i=0;i<L;i++){
int x;
char c;
cin >> x >> c;
move.push(make_pair(x,c));
}
while(1){
int changeTime=-1;
char changeDir=-1;
if(!move.empty()){
changeTime = move.front().first;
changeDir = move.front().second;
move.pop();
}
while(times != changeTime){
times+=1;
// wall
if(row+dx[direction] > N || col+dy[direction] > N || row+dx[direction] < 1 || col+dy[direction] < 1){
cout << times << "\n";
return 0;
}
// hit myself
else if(board[row+dx[direction]][col+dy[direction]]==1){
cout << times << "\n";
return 0;
}
// eat apple
else if(apple[row+dx[direction]][col+dy[direction]]==1){
apple[row+dx[direction]][col+dy[direction]]=0;
board[row+dx[direction]][col+dy[direction]]=1;
snake.push(make_pair(row+dx[direction],col+dy[direction]));
// cout<<"snake size : "<< snake.size() << "\n";
}
// else
else{
board[row+dx[direction]][col+dy[direction]]=1;
snake.push(make_pair(row+dx[direction],col+dy[direction]));
int delx,dely;
delx = snake.front().first;
dely = snake.front().second;
board[delx][dely]=0;
snake.pop();
}
row+=dx[direction];
col+=dy[direction];
}
if(changeDir == 'D'){
direction = (direction+1)%4;
}else{
direction = (direction+4-1)%4;
}
}
return 0;
}