【HDU 3533】Escape(BFS)

来源:互联网 发布:徐州软件测试招聘 编辑:程序博客网 时间:2024/05/18 01:40

Description

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.

Input

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.

Output

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

Sample 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

Sample Output

9
Bad luck!

题目大意

一个人从(0,0)跑到(n,m),只有d点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y
问这个人能不能安全到达终点,如果可以输出最小的能量花费。
要求:
1.人不能到达炮塔所在的坐标
2.炮塔会挡住子弹
3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死
4.人可以选择停止不动
输入第一行是四个整数n,m,k,d,n,m表示地图的大小,k表示有几个炮塔,d表示能量。
接下来k行每行有一个字符c,以及四个整数t,v,x,y。字符c表示炮塔发射子弹的方向,用E,S,W,N分别表示东南西北。t表示子弹的发射间隔,v表示子弹的速度,x,y是炮塔的坐标。

思路

基本的思路依旧是BFS,需要在BFS的过程中做一些处理。我们可以枚举走到某一点后的四个方向,每次从四个方向中选一个方向,沿着这个方向去寻找第一个炮塔(如果有的话),看这个的炮塔的子弹方向是不是朝向自己以及子弹在这个时刻会不会在这个点上。枚举完四个点后都发现不会有子弹打到自己时,就把这个点入队列。
对于判重,需要定义vis[x][y][time]表示点(x,y)在time时刻的访问状态,这里需要注意的是这个数组不能开int,只能开bool,虽然效果好像是一样的,但是开int会爆内存,因为int占4byte,而bool只占1byte。
代码注释和题目大意大部分来自:http://blog.csdn.net/libin56842/article/details/41909459

代码

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn=100+5;int dir[5][2]={{1,0},{0,1},{0,0},{-1,0},{0,-1}};bool vis[maxn][maxn][1005];int m,n,k,d;struct proc{    int x,y,step;};struct bullet//炮塔{    char c;    int t,v;}s[maxn][maxn];int bfs(int x,int y){    int flag,dis;    proc vw,vn;    queue<proc> q;    vw.x=x;    vw.y=y;    vw.step=0;    vis[x][y][0]=1;    q.push(vw);    while(!q.empty())    {        vw=q.front();        q.pop();        if(vw.step>d) break;//如果步数已经大于最大能量还是没有找到则可以返回了。        if(vw.x==n&&vw.y==m)        {            return vw.step;        }        for(int i=0;i<5;i++)        {            vn.x=vw.x+dir[i][0];            vn.y=vw.y+dir[i][1];            vn.step=vw.step+1;            if(vn.x>=0&&vw.x<=n&&vw.y>=0&&vw.y<=m)//判断越界            {                if(!s[vn.x][vn.y].t&&vn.step<=d&&!vis[vn.x][vn.y][vn.step])//当前点不在炮塔上并且步数小于等于最大能量并且这个时刻这个点未被访问                {                    flag=1;                    for(int j=vn.x-1;j>=0;j--)//从这个点往北(x减小方向)寻找是否有炮塔                    {                        if(s[j][vn.y].t&&s[j][vn.y].c=='S')//找到炮塔,并且这个炮塔子弹的发射方向朝向自己(子弹方向向南)                        {                            dis=vn.x-j;//炮塔和当前点的距离                            if(dis%s[j][vn.y].v) break//因为不需要看子弹中途的点,子弹每一秒跑v,距离是dis,dis不能整除v的话,那么子弹是不可能停在这个点的                              int tmp=vn.step-(dis/s[j][vn.y].v);//人走的时间减去第一个子弹飞行到这个位置所需的时间                              if(tmp<0) break;//为负数就是第一个子弹都没有经过这个点,那么人绝对安全                              if(tmp%s[j][vn.y].t==0)//看间隔,能整除,那么就是后续有子弹刚好到这个点,人死定了                              {                                flag=0;                                break;                            }                        }                        if(s[j][vn.y].t) break;//找到炮台但不是朝南射击,那么这个炮台会当下后面所有子弹,所以北方向安全我们不需要再找                      }                    if(!flag) continue;//这个方向都死定了,后面也就不需要看了                      //其他方向也是一样的道理,就不注释了                      for(int j=vn.x+1;j<=n;j++)                    {                        if(s[j][vn.y].t&&s[j][vn.y].c=='N')                        {                            dis=j-vn.x;                            if(dis%s[j][vn.y].v) break;                            int tmp=vn.step-(dis/s[j][vn.y].v);                            if(tmp<0) break;                            if(tmp%s[j][vn.y].t==0)                            {                                flag=0;                                break;                            }                        }                        if(s[j][vn.y].t) break;                    }                    if(!flag) continue;                     for(int j=vn.y-1;j>=0;j--)                    {                        if(s[vn.x][j].t&&s[vn.x][j].c=='E')                        {                            dis=vn.y-j;                            if(dis%s[vn.x][j].v) break;                            int tmp=vn.step-(dis/s[vn.x][j].v);                            if(tmp<0) break;                            if(tmp%s[vn.x][j].t==0)                            {                                flag=0;                                break;                            }                        }                        if(s[vn.x][j].t) break;                    }                    if(!flag) continue;                     for(int j=vn.y+1;j<=m;j++)                    {                        if(s[vn.x][j].t&&s[vn.x][j].c=='W')                        {                            dis=j-vn.y;                            if(dis%s[vn.x][j].v) break;                            int tmp=vn.step-(dis/s[vn.x][j].v);                            if(tmp<0) break;                            if(tmp%s[vn.x][j].t==0)                            {                                flag=0;                                break;                            }                        }                        if(s[vn.x][j].t) break;                    }                    if(!flag) continue;                     vis[vn.x][vn.y][vn.step]=1;                    q.push(vn);                     }            }        }    }    return -1;}int main(){    char c;    int t,v,x,y;    while(~scanf("%d %d %d %d",&n,&m,&k,&d))    {        memset(vis,0,sizeof(vis));        memset(s,0,sizeof(s));        for(int i=1;i<=k;i++)        {            cin>>c;            scanf("%d %d %d %d",&t,&v,&x,&y);            s[x][y].c=c;            s[x][y].v=v;            s[x][y].t=t;        }        int ans=bfs(0,0);        if(ans!=-1) printf("%d\n",ans);        else printf("Bad luck!\n");    }       return 0;}
0 0
原创粉丝点击