HDU 3533 Escape BFS

Escape

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2183    Accepted Submission(s): 639

Problem 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)到(m,n),只有p点能量,一秒消耗一点能量,逃往图中有k个炮塔,它们只能朝一个方向发射,时间间隔为t,问能否安全撤离,并输出最短时间,否则输出Bad luck!

要求:

  • 有炮塔的地方不能走
  • 炮塔会挡住子弹
  • 炮弹只能落在整数点上
  • 人可以不动

AC代码:

#include
#include
#include
#include
#define N 110
using namespace std;
struct shot{
char dir;
int t,v;
};
struct node{//间谍的位置及路径
int x,y;
int step;
};
bool vis[N][N][1005];//注意要用bool否则会超内存
int n,m,k,p;
shot mp[N][N];
int dir[5][2]={0,1,1,0,0,-1,-1,0,0,0};
int judge(node a){
if(a.x>=0&&a.x<=m&&a.y>=0&&a.y<=n)
    return 1;
return 0;
}
int BFS(int x,int y){
int flag;
queueq;
node s,now,next;
s.x=x;
s.y=y;
s.step=0;
vis[0][0][0]=true;
q.push(s);
while(!q.empty()){
    now=q.front();
    q.pop();
    if(now.step>p)
        break;
    if(now.x==m&&now.y==n){
        return now.step;
    }
    for(int i=0;i<5;i++){
        next.x=now.x+dir[i][0];
        next.y=now.y+dir[i][1];
        next.step=now.step+1;
        if(!judge(next))continue;
        if(mp[next.x][next.y].t==0&&!vis[next.x][next.y][next.step]&&next.step<=p){
            flag=1;
            for(int j=next.x+1;j<=m;j++){//向下搜看是否有方向是朝北的
                if(mp[j][next.y].t!=0&&mp[j][next.y].dir=='N'){
                        int dis=j-next.x;
                        if(dis%mp[j][next.y].v!=0)//判断是否是整数点
                            break;
                        int t=next.step-dis/mp[j][next.y].v;
                        if(t<0)//如果小于0说明第一发炮弹没有击中
                            break;
                        if(t%mp[j][next.y].t==0){//如果刚好整除,那说明正好被击中
                            flag=0;
                            break;
                        }

                    }
                if(mp[j][next.y].t!=0)//找到炮塔,但是方向不是N,那么一定打不到
                    break;

                }
                if(!flag)continue;//如果已经击中,直接搜下一个点
           for(int j=next.x-1;j>=0;j--){
                if(mp[j][next.y].t!=0&&mp[j][next.y].dir=='S'){
                        int dis=next.x-j;
                        if(dis%mp[j][next.y].v!=0)
                            break;
                        int t=next.step-dis/mp[j][next.y].v;
                        if(t<0)
                            break;
                        if(t%mp[j][next.y].t==0){
                            flag=0;
                            break;
                        }

                    }
                if(mp[j][next.y].t!=0)
                    break;

                }
                if(!flag)continue;
            for(int j=next.y+1;j<=n;j++){
                if(mp[next.x][j].t!=0&&mp[next.x][j].dir=='W'){
                        int dis=j-next.y;
                        if(dis%mp[next.x][j].v!=0)
                            break;
                        int t=next.step-dis/mp[next.x][j].v;
                        if(t<0)
                            break;
                        if(t%mp[next.x][j].t==0){
                            flag=0;
                            break;
                        }

                    }
                if(mp[next.x][j].t!=0)
                    break;

                }
                if(!flag)continue;
             for(int j=next.y-1;j>=0;j--){
                if(mp[next.x][j].t!=0&&mp[next.x][j].dir=='E'){
                        int dis=next.y-j;
                        if(dis%mp[next.x][j].v!=0)
                            break;
                        int t=next.step-dis/mp[next.x][j].v;
                        if(t<0)
                            break;
                        if(t%mp[next.x][j].t==0){
                            flag=0;
                            break;
                        }

                    }
                if(mp[next.x][j].t!=0)
                    break;

                }
                if(!flag)continue;
                vis[next.x][next.y][next.step]=true;
                q.push(next);
            }
        }
    }
    return -1;
}
int main(){
int a,b,c,d;
char ch;
while(~scanf("%d%d%d%d",&m,&n,&k,&p)){
    memset(mp,0,sizeof(mp));
    memset(vis,false,sizeof(vis));
    while(k--){
        cin>>ch>>a>>b>>c>>d;
        mp[c][d].t=a;
        mp[c][d].v=b;
        mp[c][d].dir=ch;
    }
    int ans=BFS(0,0);
    if(ans!=-1)
        printf("%d\n",ans);
    else
       printf("Bad luck!\n");
}
return 0;
}

 

   

转载于:https://www.cnblogs.com/by-DSL/p/8666973.html

你可能感兴趣的:(HDU 3533 Escape BFS)