-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1.cpp
More file actions
74 lines (56 loc) · 1.31 KB
/
Copy path1.cpp
File metadata and controls
74 lines (56 loc) · 1.31 KB
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
#include <cstdio>
#include <deque>
using namespace std;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
struct coord {
int y;
int x;
};
int N, K, L, board[105][105], dir[10005], d;
deque<coord> dq;
bool isValidYx(int y, int x) {
return 0 < y && y <= N && 0 < x && x <= N;
}
bool isCollideWithSelf(int y, int x) {
for (auto cell: dq)
if (y == cell.y && x == cell.x)
return true;
return false;
}
int main() {
dq.emplace_back(coord{1, 1});
scanf("%d%d", &N, &K);
int yy, xx;
for (int i = 0; i < K; ++i) {
scanf("%d %d", &yy, &xx);
board[yy][xx] = 1;
}
scanf("%d", &L);
int tt;
char dd;
for (int i = 0; i < L; ++i) {
scanf("%d %c", &tt, &dd);
dir[tt] = dd;
}
int sec;
for (sec = 1;; ++sec) {
int ny = dq.front().y + dy[d];
int nx = dq.front().x + dx[d];
if (!isValidYx(ny, nx))
break;
if (isCollideWithSelf(ny, nx))
break;
dq.emplace_front(coord{ny, nx});
if (board[ny][nx] == 1)
board[ny][nx] = 0;
else
dq.pop_back();
if (dir[sec] == 'D')
d = (d + 1) % 4;
else if (dir[sec] == 'L')
d = (d + 3) % 4;
}
printf("%d\n", sec);
return 0;
}