1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string.h>
using std::vector;
using std::queue;
typedef struct {
int x;
int y;
}pos;
typedef struct {
int sec;
char degree;
}moving;
typedef struct {
int x;
int y;
}di;
int main(int argc, char* argv[]) {
int n, k,l;
int** map;
int second=0;
int snakedirection;
di move[4] = { {-1,0},{0,1},{1,0},{0,-1} };
queue<moving> qu;
vector<pos> snake;
scanf("%d %d", &n, &k);
map = new int* [n];
for (int i = 0; i < n; i++) {
map[i] = new int[n];
memset(map[i], 0, sizeof(int) * n);
}
for (int i = 0; i < k; i++) {
int x, y;
scanf("%d %d", &x, &y);
x--;
y--;
map[x][y] = 1;
}
scanf("%d", &l);
for (int i = 0; i < l; i++) {
moving temp;
scanf("%d %c", &temp.sec, &temp.degree);
qu.push(temp);
}
snake.push_back({ 0,0});
map[0][0] = 2;
snakedirection = 1;
while (1) {
int frontx, fronty;
int posx, posy;
second++;
frontx = snake[snake.size() - 1].x;
fronty = snake[snake.size() - 1].y;
if (frontx + move[snakedirection].x < 0 || frontx + move[snakedirection].x > n - 1 ||
fronty + move[snakedirection].y < 0 || fronty + move[snakedirection].y > n - 1 ||
map[frontx + move[snakedirection].x][fronty + move[snakedirection].y] == 2) {
break;
}
if (map[frontx + move[snakedirection].x][fronty + move[snakedirection].y] == 1) {
map[frontx + move[snakedirection].x][fronty + move[snakedirection].y] = 2;
snake.push_back({ frontx + move[snakedirection].x ,fronty + move[snakedirection].y});
}
else if (map[frontx + move[snakedirection].x][fronty + move[snakedirection].y] == 0) {
map[frontx + move[snakedirection].x][fronty + move[snakedirection].y] = 2;
snake.push_back({ frontx + move[snakedirection].x ,fronty + move[snakedirection].y });
posx = snake.front().x;
posy = snake.front().y;
map[posx][posy] = 0;
snake.erase(snake.begin());
}
if (qu.front().sec == second) {
if (qu.front().degree == 'L') {
snakedirection--;
if (snakedirection == -1)
snakedirection = 3;
}
else if (qu.front().degree == 'D') {
snakedirection++;
if (snakedirection == 4)
snakedirection = 0;
}
qu.pop();
}
}
printf("%d", second);
for (int i = 0; i < n; i++) {
delete[] map[i];
}
delete[] map;
return 0;
}
|
'기타 코딩 > PS' 카테고리의 다른 글
[백준] 가장 큰 정사각형 - 1915번 (0) | 2020.10.22 |
---|---|
[백준] 행렬 곱셈 순서 - 11049번 (0) | 2020.10.20 |
[백준] 테트로미노 - 14500번 (0) | 2020.10.20 |
[백준] 2048 (EASY) - 12100번 (0) | 2020.10.20 |
[백준] 구슬 탈출2 - 13460번 (0) | 2020.10.20 |