Escape

传送门HDU3533

描述

The students of the HEU are maneuvering for their military training.
The red army and the blue army are at war today. The blue army finds that Little A is the spy of the red army, so Little A has to escape from the headquarters of the blue army to that of the red army. The battle field is a rectangle of size m*n, and the headquarters of the blue army and the red army are placed at (0, 0) and (m, n), respectively, which means that Little A will go from (0, 0) to (m, n). The picture below denotes the shape of the battle field and the notation of directions that we will use later.
The blue army is eager to revenge, so it tries its best to kill Little A during his escape. The blue army places many castles, which will shoot to a fixed direction periodically. It costs Little A one unit of energy per second, whether he moves or not. If he uses up all his energy or gets shot at sometime, then he fails. Little A can move north, south, east or west, one unit per second. Note he may stay at times in order not to be shot.
To simplify the problem, let’s assume that Little A cannot stop in the middle of a second. He will neither get shot nor block the bullet during his move, which means that a bullet can only kill Little A at positions with integer coordinates. Consider the example below. The bullet moves from (0, 3) to (0, 0) at the speed of 3 units per second, and Little A moves from (0, 0) to (0, 1) at the speed of 1 unit per second. Then Little A is not killed. But if the bullet moves 2 units per second in the above example, Little A will be killed at (0, 1).
Now, please tell Little A whether he can escape.

输入

For every test case, the first line has four integers, m, n, k and d (2<=m, n<=100, 0<=k<=100, m+ n<=d<=1000). m and n are the size of the battle ground, k is the number of castles and d is the units of energy Little A initially has. The next k lines describe the castles each. Each line contains a character c and four integers, t, v, x and y. Here c is ‘N’, ‘S’, ‘E’ or ‘W’ giving the direction to which the castle shoots, t is the period, v is the velocity of the bullets shot (i.e. units passed per second), and (x, y) is the location of the castle. Here we suppose that if a castle is shot by other castles, it will block others’ shots but will NOT be destroyed. And two bullets will pass each other without affecting their directions and velocities.
All castles begin to shoot when Little A starts to escape.
Proceed to the end of file.

输出

If Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.

样例

  • Input
    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

  • Output
    9
    Bad luck!

题解

  • 题意:人要在规定时间内从(0,0)走到(m,n),每次可以上下左右四个方向走,地方有炮台进行攻击,炮台每t秒攻击一次,每次攻击每秒弹v格,只能打到弹到的格子,如果弹射过程中遇到其他炮台则会被挡住(在空中也会被挡住),人在移动时不能被炮弹打到,也不能走到炮台的位置,但是可以站在原地等炮弹过去,求人到达目的地的最短时间,超出规定时间或被炮台击中输出Bad luck!
    输入m n k d(m*n的格子,k个炮台,规定时间d),接下来k行输入炮台的射击方向,射击周期,移动速度v,炮台位置
  • 判断人在time1时刻是否会被炮台打到时需要考虑炮台的射击方向、炮弹是否会被挡住,如果人站在炮台射击方向上并且炮弹不会被其他炮弹挡住的话,判断人与炮弹之间的距离是否是v的倍数,不是则不会被击中,如果是,则计算炮弹到达人位置的时间time2,如果(time1-time2)%t!=0,则不会被击中

Code

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
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e2+7;
const int mod=1e9+7;
int m,n,k,d;
int vis[N][N][N];
int rec[5][2]={1,0,-1,0,0,1,0,-1,0,0};
struct node {
char c;
int fx,fy;//最远可达
int t,v,x,y;
}pao[N];
struct man{
int x,y,time;
};
void init(int i,int j,int q){
for(int p=0;p<k;p++){
if(p==q)continue;
if((pao[p].c=='N'||pao[p].c=='S') && j!=pao[p].y) continue;
if((pao[p].c=='W'||pao[p].c=='E') && i!=pao[p].x) continue;
//在射击路线上即挡住
if((pao[p].x-i)*(i-pao[p].fx)>=0&&(pao[p].y-j)*(j-pao[p].fy)>=0){
pao[p].fx=i,pao[p].fy=j;
}
}
}
bool judge(int i,int j,int time){
if(time>d) return false;
if(i<0||i>m||j<0||j>n)return false;
for(int p=0;p<k;p++){
if(i==pao[p].x && j==pao[p].y)return false;
if((pao[p].c=='N'||pao[p].c=='S') && j!=pao[p].y) continue;
if((pao[p].c=='W'||pao[p].c=='E') && i!=pao[p].x) continue;
if((pao[p].x-i)*(i-pao[p].fx)<0||(pao[p].y-j)*(j-pao[p].fy)<0)continue;
int dis=abs(i-pao[p].x)+abs(j-pao[p].y);
if(dis % pao[p].v!=0) continue;
if(time<dis/pao[p].v) continue;
if((time-dis/pao[p].v)%pao[p].t==0) return false;
}
return true;
}
int bfs(){
queue<man> que;
que.push((man){0,0,0});
vis[0][0][0]=1;
while(!que.empty()){
man q=que.front();que.pop();
if(q.x==m && q.y==n) return q.time;

for(int i=0;i<5;i++){
int tx=q.x+rec[i][0],ty=q.y+rec[i][1];
if(!vis[tx][ty][q.time+1]&&judge(tx,ty,q.time+1)){
que.push((man){tx,ty,q.time+1});
vis[tx][ty][q.time+1]=1;
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d%d%d",&m,&n,&k,&d)){
INIT(vis,0);
for(int i=0;i<k;i++){
getchar();
scanf("%c%d%d%d%d",&pao[i].c,&pao[i].t,&pao[i].v,&pao[i].x,&pao[i].y);
if(pao[i].c=='N')pao[i].fx=0,pao[i].fy=pao[i].y;
if(pao[i].c=='S')pao[i].fx=m,pao[i].fy=pao[i].y;
if(pao[i].c=='E')pao[i].fx=pao[i].x,pao[i].fy=n;
if(pao[i].c=='W')pao[i].fx=pao[i].x,pao[i].fy=0;
}
for(int i=0;i<k;i++){
init(pao[i].x,pao[i].y,i);
}
int ans=bfs();
if(ans==-1)printf("Bad luck!\n");
else printf("%d\n",ans);
}

return 0;
}
Donate comment here