https://www.acmicpc.net/problem/3190
그냥 냅다 구현이다. 근데 지문을 잘 읽어야 함
board[i][j] == 0 빈칸인 경우로, 뱀이 이곳에 온다면 머리를 옮기고 꼬리를 땡겨주어야 한다.board[i][j] == 1 사과가 있는 경우로, 뱀이 이곳에 오면 머리만 옮겨주면 된다.board[i][j] == -1 뱀의 몸통이 있는 자리이다. 뱀이 이곳에 오면 게임 끝char l[10001]l[i] : i 초에서 바꾸어야 할 방향d_idxD : ( d_idx + 1 ) % 4L : ( d_idx + 3 ) % 4| 👉 | 👇 | 👈 | 👆 | |
|---|---|---|---|---|
| dx | 0 | 1 | 0 | -1 |
| dy | 1 | 0 | -1 | 0 |
deque에 저장하는 것이었다.난 while(1)에서 종료조건을 맞추기가 왜이렇게 힘든지 모르겠다 ^^,,
그래서 테스트케이스 2번의 답이 왜 20이 아니라 21인지 이해가 안됐다 ^.ㅜ
time은 0이다. 방향 변환을 먼저 하고 이동을 시켰다
다음 L개의 줄에는 뱀의 방향 변환 정보가 주어지는데, 정수 X와 문자 C로 이루어져 있으며. 게임 시작 시간으로부터 X초가 끝난 뒤에 왼쪽(C가 'L') 또는 오른쪽(C가 'D')로 90도 방향을 회전시킨다는 뜻이다.
방향 변환은 이동한 "후" 이루어져야 하는 동작이다.. 문제를 잘 읽자
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define pii pair<int,int>
int N,K,L;
const int n_ = 101;
int board[n_][n_]; // 1: 사과 0: 빈칸 -1: 뱀
char l[10001];
int d_idx;
deque<pii> s;
const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};
bool in_board(int x, int y){
return (0<=x && x<N && 0<=y && y<N);
}
void print_board(){
cout<<"-----";
switch(d_idx){
case 0 : cout<<"→"; break;
case 1 : cout<<"↓"; break;
case 2 : cout<<"←"; break;
case 3 : cout<<"↑"; break;
}
cout<<"-----\n";
for(int i = 0; i<N; i++){
for(int j = 0; j<N; j++){
if(s.front().first == i && s.front().second == j) cout<<"*";
cout<<board[i][j]<<"\t";
}
cout<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>N>>K;
for(int i = 0; i<K; i++){
int x,y; cin>>x>>y;
board[x-1][y-1] = 1;
}
cin>>L;
for(int i = 0; i<L; i++){
int x; char c;
cin>>x>>c;
l[x] = c;
}
d_idx = 0;
s.push_front({0,0});
board[0][0] = -1;
int time = 0;
while(1){
pii head = s.front();
int x = head.first;
int y = head.second;
//현재 머리가 보드 밖 or 자신의 몸 -> break
if(!in_board(x,y)) {cout<<time<<"\n"; break;}
int nx = x + dx[d_idx];
int ny = y + dy[d_idx];
//머리 이동
if(board[nx][ny] == -1) {cout<<time+1<<"\n"; break;}
if(board[nx][ny] == 0) {
pii tail = s.back();
board[tail.first][tail.second] = 0;
s.pop_back();
}
board[nx][ny] = -1;
s.push_front({nx,ny});
time++;
//방향결정
if(l[time] == 'L') d_idx = (d_idx+3)%4;
else if(l[time] == 'D') d_idx = (d_idx+1)%4;
}
}
