HDU-2533:Escape

来源:互联网 发布:威尼斯 知乎 编辑:程序博客网 时间:2024/06/15 20:12
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 10N 1 1 1 1W 1 1 3 2W 2 1 2 44 4 3 10N 1 1 1 1W 1 1 3 2W 1 1 2 4
Sample Output
9Bad luck!

注意四点:

1.人不能到达炮塔所在的坐标

2.炮塔会挡住子弹

3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死

4.人可以选择停止不动

那么剩下的就是简单BFS了,还有由于数组较大,开int会MLE所以开bool

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 102;bool vist[maxn][maxn][1002];int m,n,k,d,t,v,x,y;int dx[5]={0,0,0,1,-1};int dy[5]={0,1,-1,0,0};char C[3];struct castles{//记录碉堡的信息    int v,t;    int is;    char c;}Point[maxn][maxn];struct node{//记录人的状态    int x,y;    int step;}U,V;bool judge(node a){//判断该位置是否合法    if(a.x>=0 && a.x<=n && a.y>=0 && a.y <= m && !vist[a.x][a.y][a.step])        return true;    return false;}void BFS(){    U.x = U.y = U.step = 0;    queue<node>Q;    Q.push(U);    vist[0][0][0] = true;    int flag,dis,cnt;    while(!Q.empty()){        U = Q.front();        Q.pop();        if(U.step>d)//人的能量为零就结束            break;        if(U.x == n && U.y == m){//可以顺利回到红军基地            printf("%d\n",U.step);            return;        }        for(int i = 0; i < 5; i++){//枚举5种状态            V.x = U.x+dx[i];            V.y = U.y+dy[i];            V.step = U.step+1;            if(!judge(V))                continue;            if(!Point[V.x][V.y].is && V.step <= d){//碉堡的位置不能走,人没能量不能走                flag = 1;                for(int j = V.x-1; j >= 0; j--){                    if(Point[j][V.y].is && Point[j][V.y].c == 'S'){//人的北方第一个射击方向为南的碉堡                        dis = V.x-j;//人与碉堡的距离                        if(dis%Point[j][V.y].v == 0){//这个位置可能会被射击到                            cnt = V.step-dis/Point[j][V.y].v;//时间差                            if(cnt < 0)//子弹还没到                                break;                            if(cnt%Point[j][V.y].t == 0){//射死                                flag = 0;                                break;                            }                        }                        else//如果死不了就不用枚举这个方向其他的了碉堡了,因为子弹无法射过碉堡(还有这种操作)!                            break;                    }                    if(Point[j][V.y].is)//如果第一个碉堡方向不为南也不用枚举这个方向其他的了碉堡了,理由同上。                        break;                }                if(!flag)//如果死了就不用枚举下面的方向的情况了。                    continue;                for(int j = V.x+1; j <= n; j++){                    if(Point[j][V.y].is && Point[j][V.y].c == 'N'){                        dis = j-V.x;                        if(dis%Point[j][V.y].v == 0){                            cnt = V.step-dis/Point[j][V.y].v;                            if(cnt < 0)                                break;                            if(cnt%Point[j][V.y].t == 0){                                flag = 0;                                break;                            }                        }                        else                            break;                    }                    if(Point[j][V.y].is)                        break;                }                if(!flag)                    continue;                for(int j = V.y-1; j >= 0; j--){                    if(Point[V.x][j].is && Point[V.x][j].c == 'E'){                        dis = V.y-j;                        if(dis%Point[V.x][j].v == 0){                            cnt = V.step-dis/Point[V.x][j].v;                            if(cnt < 0)                                break;                            if(cnt%Point[V.x][j].t == 0){                                flag = 0;                                break;                            }                        }                        else                            break;                    }                    if(Point[V.x][j].is)                        break;                }                if(!flag)                    continue;                for(int j = V.y+1; j <= m; j++){                    if(Point[V.x][j].is && Point[V.x][j].c == 'W'){                        dis = j-V.y;                        if(dis%Point[V.x][j].v == 0){                            cnt = V.step-dis/Point[V.x][j].v;                            if(cnt < 0)                                break;                            if(cnt%Point[V.x][j].t == 0){                                flag = 0;                                break;                            }                        }                        else                            break;                    }                    if(Point[V.x][j].is)                        break;                }                if(!flag)                    continue;                vist[V.x][V.y][V.step] = true;                Q.push(V);            }        }    }    puts("Bad luck!");}int main(){    while(~scanf("%d %d %d %d",&n,&m,&k,&d)){        memset(Point,0,sizeof(Point));        memset(vist,false,sizeof(vist));        for(int i = 0; i < k; i++){            scanf("%s %d %d %d %d",C,&t,&v,&x,&y);            Point[x][y].t = t;//子弹的射击周期            Point[x][y].v = v;//子弹的速度            Point[x][y].is = 1;//是否是碉堡            Point[x][y].c = C[0];        }        BFS();    }    return 0;}


原创粉丝点击