알고리즘/백준
3190. 뱀
혬혬
2020. 10. 14. 23:54
728x90
#include <iostream>
#include <queue>
using namespace std;
typedef struct y {
int i;
int j;
};
int map[110][110] = { 0 };
int list[110][2];
int head[2] = {1,1};
int tail[2] = {1,1};
int d = 1;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
int main() {
freopen("inp.inp", "r", stdin);
freopen("out.out", "w", stdout);
int n = 0;
cin >> n;
int apple;
cin >> apple;
for (int i = 0; i < apple; i++) {
int a, b;
cin >> a >> b;
map[a][b] = 5;
}
int amount = 0;
cin >> amount;
for (int i = 0; i < amount; i++) {
int a;
char b;
cin >> a;
cin >> b;
list[i][0] = a;
if (b == 'D')
list[i][1] = 1;
else
list[i][1] = -1;
}
int point = 0;
int time = 0;
map[head[0]][head[1]] = 1;
queue<y> qu;
//qu.push({ 1,1 });
while (1) {
if (time == list[point][0]) {
d += list[point++][1];
if (d < 0) {
d = 3;
}
if (d > 3)
d = 0;
}
time++;
head[0] += dx[d];
head[1] += dy[d];
qu.push({ head[0],head[1] });
if (map[head[0]][head[1]] == 1) {
break;
}
if (map[head[0]][head[1]] == 0) {
map[tail[0]][tail[1]] = 0;
y i = qu.front();
qu.pop();
tail[0] = i.i;
tail[1] = i.j;
}
map[head[0]][head[1]] = 1;
if (head[0] < 1 || head[0] > n || head[1] < 1 || head[1] > n)
break;
if (tail[0] < 1 || tail[0] > n || tail[1] < 1 || tail[1] > n)
break;
}
cout << time;
return 0;
}
728x90