HDU 3533 Escape (预处理+BFS)

16 篇文章 0 订阅

题目

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!

题目链接

HDU_3533_Escape

参考链接

HDU_3533_Escape_Discuss Board

题目简述

三维判重的迷宫搜索问题,读完题面有以下几点需要注意:

1. 不能走到敌人的城堡!即使堡垒此时不往外发射子弹,也不可以走到敌人的堡垒!

2.这是一个(n+1)*(m+1)大小的迷宫,不是n*m大小的迷宫。

3.题面图中的n、m写反了!(一开始没注意,可以通过样例。真按照图中做,反而通过不了样例了)。

4.如果此时站在此点上被射击到,需要满足哪些条件。

5.Little A可以朝东西南北四个方向走,也可以站在原地不动。

程序分析

int castle_num/**城堡数量**/,n,m,k,d;
int zx[]={-1,0,1,0,0};
int zy[]={0,1,0,-1,0};/**方向数组**/
bool visit[maxn][maxn][maxn];
struct MAP
{
    int castle;
    int sour_bul[4];/**Source of bullets**/
}Map[maxn][maxn];

struct Castle
{
    char dir/**direction**/;
    int t/**周期**/,v/**速度**/,x,y;
}ca[1005];/**城堡数组**/

struct Node/**状态**/
{
    int x,y,time;
};

void init()/**初始化数组**/
bool be_shot(Node a)/**判断该状态是否会吃子弹**/
void mark()/**预处理**/
int bfs()/**搜索**/
 

程序

#include<iostream>
#include<stdio.h>
#include<queue>
#include<cmath>
#include<string.h>
#include<math.h>
using namespace std;
#define maxn 105
int castle_num,n,m,k,d;
int zx[]={-1,0,1,0,0};
int zy[]={0,1,0,-1,0};

bool visit[maxn][maxn][maxn];

struct MAP
{
    int castle;
    int sour_bul[4];/**Source of bullets**/
}Map[maxn][maxn];

struct Castle
{
    char dir/**direction**/;
    int t/**周期**/,v/**速度**/,x,y;
}ca[1005];

struct Node
{
    int x,y,time;
};

void init()
{
    memset(visit,0,sizeof(visit));
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            Map[i][j].castle=-1;
            for(int k=0;k<4;k++)
                Map[i][j].sour_bul[k]=-1;
        }
    }
}

bool be_shot(Node a)
{
    if(Map[a.x][a.y].castle!=-1/*&&a.time%ca[Map[a.x][a.y].castle].t==0**/)
        return true;
    for(int i=0;i<4;i++)
    {
        int num=Map[a.x][a.y].sour_bul[i];
        if(num!=-1)
        {
            int dist=abs(ca[num].x-a.x)+abs(ca[num].y-a.y);
            if(dist%ca[num].v==0)
            {
                int tim=a.time-dist/ca[num].v;
                if(tim>=0&&tim%ca[num].t==0)
                    return true;
            }
        }
    }
    return false;
}

void mark()
{
    for(int i=0;i<castle_num;i++)
        Map[ca[i].x][ca[i].y].castle=i;
    for(int i=0;i<castle_num;i++)
    {
        int direc,xx,yy;
        if(ca[i].dir=='N') direc=0;
        else if(ca[i].dir=='E') direc=1;
        else if(ca[i].dir=='S') direc=2;
        else if(ca[i].dir=='W') direc=3;
        for(xx=ca[i].x,yy=ca[i].y;;xx+=zx[direc],yy+=zy[direc])
        {
            if(xx<0||xx>n||yy>m||yy<0)
                break;
            if((xx!=ca[i].x||yy!=ca[i].y)&&Map[xx][yy].castle!=-1)
                break;
            Map[xx][yy].sour_bul[direc]=i;
        }
    }
}



int bfs()
{
    Node start;
    start.time=0;
    start.x=0;
    start.y=0;
    queue<Node>q;
    q.push(start);
    visit[start.x][start.y][start.time]=true;
    while(!q.empty())
    {
        Node now=q.front();
        q.pop();
        for(int i=0;i<5;i++)
        {
            Node next=now;
            next.time++;
            next.x+=zx[i];
            next.y+=zy[i];
            if(next.x>=0&&next.x<=n&&next.y>=0&&next.y<=m&&!visit[next.x][next.y][next.time])
            {
                if(!be_shot(next)&&next.time<=d)
                {
                    visit[next.x][next.y][next.time]=true;
                    q.push(next);
                    if(next.x==n&&next.y==m)
                        return next.time;
                }
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d%d%d",&n,&m,&k,&d)!=EOF)
    {
        init();
        castle_num=k;
        char c;
        for(int i=0;i<castle_num;i++)
        {
            scanf("%c",&c);
            scanf("%c",&ca[i].dir);
            scanf("%d%d%d%d",&ca[i].t,&ca[i].v,&ca[i].x,&ca[i].y);
        }
        mark();
        int answer=bfs();
        if(answer==-1)
            printf("Bad luck!\n");
        else
            printf("%d\n",answer);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值