HDU 3533 Escape

题目链接

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!

大致题意
给你一个n*m矩阵,接下来k和d表示塔的数量和d的时间
接下来k行,每行s,t,v,x,y,分别表示塔防的攻击方向,攻击时间间隔,子弹速度,和塔防坐标,问是否可以在不被击中的情况下从左上角走到左下角

具体思路
1.走的时候可以停留在原地浪费一秒
2.不能走到塔的格子
3.当子弹和人同时到同一个格子的时候认为被攻击
4.子弹飞到格子的时间默认整数,小数就是打不到人
5,塔可以挡子弹
其实只要计算出在d的时间内,这个矩阵上子弹会飞到哪个格子就可以了,一个bfs的预处理去存取子弹的出现格子与时间即可
注意:标记用bool的数组,int本人爆内存了好几发,注意多组数据初始化标记,这个我又WA了两个,很头疼

#include<bits/stdc++.h>
using namespace std;
struct T
{
    int s,t,v,x,y;
}turret[105];
struct node
{
    int x,y,times;
};
int n,m,k,d;
int change[5][2]={{-1,0},{1,0},{0,1},{0,-1},{0,0}};
bool flags[105][105][1005],pot[105][105],used[105][105][1005];
void pre()
{
    memset(flags,0,sizeof(flags));
    for (int i=0;i<k;i++)
    {
        for (int j=0;j<=d;j=j+turret[i].t)
        {
            for (int k=1;;k++)
            {
                int x=turret[i].x+change[turret[i].s][0]*k;
                int y=turret[i].y+change[turret[i].s][1]*k;
                if (x<0||x>n||y<0||y>m||pot[x][y])
                {
                    break;
                }
                if (k%turret[i].v==0)
                {
                    flags[x][y][j+k/turret[i].v]=true;
                }
            }
        }
    }
}
int bfs()
{
    memset(used,0,sizeof(used));
    queue<node>dui;
    while (!dui.empty())
    {
        dui.pop();
    }
    node start;
    start.times=start.x=start.y=0;
    used[0][0][0]=true;
    dui.push(start);
    while (!dui.empty())
    {
        node now;
        now=dui.front();
        dui.pop();
        if (now.times>d)
        {
            return -1;
        }
        if (now.x==n&&now.y==m)
        {
            return now.times;
        }
        for (int i=0;i<5;i++)
        {
            node next;
            next.x=now.x+change[i][0];
            next.y=now.y+change[i][1];
            next.times=now.times+1;
            if (next.x>=0&&next.x<=n&&next.y>=0&&next.y<=m&&!pot[next.x][next.y]
                &&!flags[next.x][next.y][next.times]&&!used[next.x][next.y][next.times])
            {
                used[next.x][next.y][next.times]=1;
                dui.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    while (scanf("%d %d %d %d%*c",&n,&m,&k,&d)!=EOF)
    {
        memset(pot,0,sizeof(pot));
        char s,v,x,y;
        for (int i=0;i<k;i++)
        {
            scanf("%c %d %d %d %d%*c",&s,&turret[i].t,&turret[i].v,&turret[i].x,&turret[i].y);
            if(s=='N')
            {
                turret[i].s=0;
            }
            if(s=='S')
            {
                turret[i].s=1;
            }
            if(s=='E')
            {
                turret[i].s=2;
            }
            if(s=='W')
            {
                turret[i].s=3;
            }
            pot[turret[i].x][turret[i].y]=1;
        }
        pre();
        int ans=bfs();
        if (ans==-1)
        {
            printf("Bad luck!\n");
        }else
        {
            printf("%d\n",ans);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值