Escape HDU - 3533(小A的逃跑计划+四次BFS+搜索难度:中)

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!

题意:

小A要从(0,0)位置走到(m,n),在逃跑过程中可能会遇到来自四面的炮弹,小A要尽量避免这些炮弹才能活着出去。

小A每走一秒消耗一格能量,当能量为0时小A over

在图中一共有k个炮台,给出每个炮台的位置,发射方向以及子弹的速度和时间

如果子弹的终点和人到达的位置坐标相等时,人才被认为是被子弹射中而over,人不能走炮台的位置,并且人是可以原地不动的,但是不动也需要消耗能量,每秒消耗1格能量

思路:

对小A进行广搜,并且判断小A走到的位置四周有没有炮台并且有没有炮台的子弹刚好射中小A即可,注意标记小A走的过程,因为时间是不同的,消耗的能量也是不同的,所以开一个数组标记即可。

代码:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int dir[5][2]= {0,1,0,-1,1,0,-1,0,0,0}; //四个方向+原地不动
bool vis[110][110][110];
int m,n,k,d,x,y,t,v;//k 城堡数量 d 小A的能量
char c[3];
struct node
{
    char c;
    int t,v;
} s[110][110];
struct node1
{
    int x,y,step;
} u,w;
int check(int x,int y)//是否越界
{
    if(x>=0&&x<=m&&y>=0&&y<=n)
        return 1;
    return 0;
}
void bfs()
{
    queue<node1>q;
    int flag,dis,t,i,j;
    u.x=0;
    u.y=0;
    u.step=0;
    q.push(u);
    vis[0][0][0]=1;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        if(u.step>d)//小A能量耗尽
            break;
        if(u.x==m&&u.y==n)//走出去了
        {
            printf("%d\n",u.step);
            return ;
        }
        for(i=0; i<5; i++)
        {
            w.x=u.x+dir[i][0];
            w.y=u.y+dir[i][1];
            w.step=u.step+1;
            if(check(w.x,w.y)==1&&s[w.x][w.y].t==0&&!vis[w.x][w.y][w.step]&&w.step<=d)//不越界&发射时间为0&未被标记&步数(耗费的能量)少于能量
            {
                flag=1;//可以走,标记活着
                for(j=w.x-1; j>=0; j--)//向北走
                {
                    if(s[j][w.y].t&&s[j][w.y].c=='S')
                    {
                        dis=w.x-j;//小A离炮台的距离,w.x是城堡的x坐标,j是小A的x坐标
                        if(dis%s[j][w.y].v)//不看子弹的过程,只看最终的结果,如果说距离对速度取余不为0,说明距离不可以整除V,那么子弹也就到不了这个位置
                            break;
                        t=w.step-dis/s[j][w.y].v;//人走的时间-子弹飞行到这个距离的时间
                        if(t<0)//为负数就说明第一个子弹都没有经过这个点
                            break;
                        if(t%s[j][w.y].t==0)//能整除说明后续会有子弹经过这个点
                        {
                            flag=0;
                            break;
                        }
                    }
                    if(s[j][w.y].t)//方向不是向南,小A永远也遇不到来自这里的子弹
                        break;
                }
                if(flag==0)//flag=0说明小A已over!
                    continue;
                //后面的同理,稍作改动即可
                for(j=w.x+1; j<=m; j++)//向南走
                {
                    if(s[j][w.y].t&&s[j][w.y].c=='N')
                    {
                        dis=j-w.x;
                        if(dis%s[j][w.y].v)
                            break;
                        t=w.step-dis/s[j][w.y].v;
                        if(t<0)
                            break;
                        if(t%s[j][w.y].t==0)
                        {
                            flag=0;
                            break;
                        }
                    }
                    if(s[j][w.y].t)
                        break;
                }
                if(flag==0)
                    continue;
                for(j=w.y-1; j>=0; j--)//向左走,西..
                {
                    if(s[w.x][j].t&&s[w.x][j].c=='E')
                    {
                        dis=w.y-j;
                        if(dis%s[w.x][j].v)
                            break;
                        t=w.step-dis/s[w.x][j].v;
                        if(t<0)
                            break;
                        if(t%s[w.x][j].t==0)
                        {
                            flag=0;
                            break;
                        }
                    }
                    if(s[w.x][j].t)
                        break;
                }
                if(flag==0)
                    continue;
                for(j=w.y+1; j<=n; j++)
                {
                    if(s[w.x][j].t&&s[w.x][j].c=='W')//向右走,东..
                    {
                        dis=j-w.y;
                        if(dis%s[w.x][j].v)
                            break;
                        t=w.step-dis/s[w.x][j].v;
                        if(t<0)
                            break;
                        if(t%s[w.x][j].t==0)
                        {
                            flag=0;
                            break;
                        }
                    }
                    if(s[w.x][j].t)
                        break;
                }
                if(flag==0)
                    continue;
                vis[w.x][w.y][w.step]=1;
                q.push(w);
            }
        }
    }
    printf("Bad luck!\n");
}
int main()
{

    while(~scanf("%d %d %d %d",&m,&n,&k,&d))
    {
        memset(s,0,sizeof(s));
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=k; i++)
        {
            scanf("%s %d %d %d %d",c,&t,&v,&x,&y);
            s[x][y].c=c[0];//方向
            s[x][y].t=t;//时间
            s[x][y].v=v;//速度
        }
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值