forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1019Illusive Chase.cpp
More file actions
104 lines (97 loc) · 2.35 KB
/
1019Illusive Chase.cpp
File metadata and controls
104 lines (97 loc) · 2.35 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
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
#include <iostream>
#include <vector>
using namespace std;
#define for if(0); else for
class move {
public:
int a;
int b;
int dir;
move(int aa, int bb, int dd) {
a = aa, b = bb, dir = dd;
}
};
class point {
public:
int x;
int y;
point() {
}
point(int xx, int yy) {
x = xx, y = yy;
}
point &operator = (const point &cc) {
x = cc.x;
y = cc.y;
return *this;
}
};
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char cc[5] = "DLUR";
int m, n;
void Run() {
cin >> m >> n;
int mat[100][100];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> mat[i][j];
}
}
vector<move> mm;
int a, b, d;
char ch;
while (cin >> a >> b) {
if (a == 0 && b == 0) break;
cin >> ch;
if (a > b) a ^= b ^= a ^= b;
for (int i = 0; i < 4; ++i) {
if (cc[i] == ch) {
d = i;
break;
}
}
mm.push_back(move(a, b, d));
}
point queue[20000];
int head = 0, tail = -1, nhead, ntail;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 0) {
queue[++tail] = point(i, j);
}
}
}
for (int ii = mm.size() - 1; ii >= 0; --ii) {
vector<vector<int> > visited(m, vector<int>(n, 0));
nhead = tail + 1, ntail = tail;
for (int i = head; i <= tail; ++i) {
int nx = queue[i].x, ny = queue[i].y;
a = mm[ii].a, b = mm[ii].b, d = mm[ii].dir;
for (int j = 1; j <= b; ++j) {
int tx = nx + j * dir[d][0];
int ty = ny + j * dir[d][1];
if (tx < 0 || tx >= m || ty < 0 || ty >= n || mat[tx][ty] == 1) break;
if (j < a) continue;
if (visited[tx][ty]) continue;
visited[tx][ty] = 1;
queue[++ntail] = point(tx, ty);
}
}
if (ntail < nhead) {
cout << 0 << endl;
return;
}
for (int i = nhead; i <= ntail; ++i) {
queue[i - nhead] = queue[i];
}
head = 0, tail = ntail - nhead;
}
cout << tail - head + 1 << endl;
}
int main() {
int kase;
for (cin >> kase; kase; --kase) {
Run();
}
return 0;
}