这个题真的把我做崩了。删删减减至少写了1000行。。。。中间打表的部分死活做不对(做了几乎整整一晚上。。。),最后借鉴了一位大神的博客,Orz,我写废了的也跟在后面了。

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma GCC optimize(2)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 100 + 2;

struct node {
int x;
int y;
int sum;///能量
};

struct p {
char chr;
int t,v,x,y;
} store_fortress[101];

bool mark_fortress[maxn][maxn];
bool mark_map[maxn][maxn][1002];
bool mark_bullet[1002][maxn][maxn];

int m,n,k,d;
char dir;
int t,v,x,y;
int ds[5][2] = {{0,0},{1,0},{0,1},{-1,0},{0,-1}};

void treaval() {
for(int t = 1; t <= d ; t++) {
printf("%d>>>\n",t);
for(int i = 0 ; i <= m ; i++) {
for(int j = 0 ; j <= n ; j++) {
printf("%d ",mark_bullet[t][i][j]);
}
printf("\n");
}
printf(">>>>>>>>>\n");
}
}

void update(char id, int t,int v, int x,int y) {

if(id == 'W') {
int stop = 0;
// for(int j = y - 1; j >= 0; j --) {
// if(mark_fortress[x][j]) {
// stop = j;
// break;
// }
// }
for(int j = y - v,ini = 1; j >= 0; j -= v,ini ++) {
for(int k = ini; k <= d; k += t) {
mark_bullet[k][x][j] = true;
}
if(mark_fortress[x][j]){
break;
}
}

} else if(id == 'E') {
for(int j = y + v,ini = 1; j <= m; j += v,ini ++) {
for(int k = ini; k <= d; k += t) {
mark_bullet[k][x][j] = true;
}
if(mark_fortress[x][j]) {
break;
}
}
} else if(id == 'N') {
for(int j = x - v,ini = 1; j >= 0; j -= v,ini ++) {
for(int k = ini; k <= d; k += t) {
mark_bullet[k][j][y] = true;
}
if(mark_fortress[j][y]) {
break;
}
}
} else if(id == 'S') {
for(int j = x + v,ini = 1; j <= n; j += v,ini ++) {
for(int k = ini; k <= d; k += t) {
mark_bullet[k][j][y] = true;
}
if(mark_fortress[j][y]) {
break;
}
}
}
}

inline bool check(int x,int y,int t) {

if(mark_map[x][y][t] == 0 && mark_bullet[t][x][y] == 0 && mark_fortress[x][y] == 0 && x >= 0 && y >= 0 && x <= m && y <= n)
return 1;
return 0;
}

queue<node>q;
void bfs() {
node p,t;
p.x = 0 ;
p.y = 0;
p.sum = 0;
q.push(p);
mark_map[0][0][0] = 1;
while(!q.empty()) {
p = q.front();
q.pop();
if(p.x == m && p.y == n) {
printf("%d\n",p.sum);
while(!q.empty()) {
q.pop();
}
return ;
}
if(p.sum > d)
continue;
for(int i = 0 ; i <= 2 ; i++) {
t.x = p.x + ds[i][0];
t.y = p.y + ds[i][1];
t.sum = p.sum + 1;
if(check(t.x, t.y, t.sum)) {
mark_map[t.x][t.y][t.sum] = 1;
q.push(t);
}
}
}
printf("Bad luck!\n");
return ;
}
int main() {

while(scanf("%d %d %d %d",&m,&n,&k,&d) != EOF) {
getchar();
memset(mark_fortress,0,sizeof mark_fortress);
memset(mark_map,0,sizeof mark_map);
memset(mark_bullet,0,sizeof mark_bullet);

// fill(&mark_fortress[0][0],&mark_fortress[maxn-1][maxn-1],false);
// fill(&mark_bullet[0][0][0],&mark_bullet[1001][maxn-1][maxn-1],false);
// fill(&mark_map[0][0][0],&mark_map[maxn][maxn-1][1001],false);

for(int i = 1 ; i <= k ; i++) {
scanf("%c %d %d %d %d",&store_fortress[i].chr,&store_fortress[i].t,&store_fortress[i].v,&store_fortress[i].x,&store_fortress[i].y);
getchar();
mark_fortress[store_fortress[i].x][store_fortress[i].y] = 1;
}

for(int i = 1; i <= k ; i++) {
update(store_fortress[i].chr,store_fortress[i].t,store_fortress[i].v,store_fortress[i].x,store_fortress[i].y);
}

bfs();

// treaval();

}
return 0;
}

/*
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 1 1 2 4
8

4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 2 1 2 4
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 1 1 2 4

*/

/*
W3
E4
N1
S2
N
S
W
E

*/

/*
void update(int a[][5]) {
for(int t = 1; t <= d ; t++) {///时间轴
for(int i = 1; i <= k ; i++) {
int id = a[i][0];///方向
int cycle = a[i][1];///周期
int one_dis = a[i][2];///速度
int num_bullet = (t-1)/cycle+1;///子弹个数
int len = t*one_dis; ///最远的子弹离堡垒的距离
int x = a[i][3];
int y = a[i][4];

if(id == 1) { ///N
// if(num_bullet * one_dis > x) {
// continue;
// }
// printf("1\n");
for(int j = x-1 ; j >= 0 ; j--) {
if(mark_fortress[j][y] == 1) {
break;
}
if(num_bullet) {
if((x-j)%one_dis != 0)
continue;
num_bullet--;
mark_bullet[t][j][y] = 1;
} else {
break;
}
}
} else if(id == 2) { ///S
// printf("2\n");
// if(num_bullet * one_dis > m - x) {
// continue;
// }
for(int j = x+1 ; j <= m ; j++) {
if(mark_fortress[j][y] == 1) {
break;
}
if(num_bullet) {
if((j-x)%one_dis != 0)
continue;
num_bullet--;
mark_bullet[t][j][y] = 1;
} else {
break;
}
}
} else if(id == 3) { ///W
// printf("3\n");
// if(num_bullet * one_dis > y) {
// continue;
// }
if(t%cycle != 0) {
for(int j = y - 1 ; j >= 0 ; j--) {
if(mark_fortress[x][j] == 1) {
break;
}
if(a[j] == 1) {
a[j] ==
}
}
}
for(int j = y-1 ; j >= 0 ; j--) {
if(mark_fortress[x][j] == 1) {
break;
}
if(num_bullet) {
num_bullet--;
mark_bullet[t][x][j] = 1;
} else {
break;
}
}
} else if(id == 4) { ///E
// printf("4\n");
// if(num_bullet * one_dis > n - y) {
// continue;
// }
for(int j = y+1 ; j <= n ; j++) {
if(mark_fortress[x][j] == 1) {
break;
}
if(num_bullet) {
if((j-y)%one_dis != 0)
continue;
num_bullet--;
mark_bullet[t][x][j] = 1;
} else {
break;
}
}
}
}
// printf("ASDF\n");
}
}

*/