HDU3533:Escape(BFS)

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)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y

问这个人能不能安全到达终点

要求: 

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

2.炮塔会挡住子弹

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

4.人可以选择停止不动

思路:其实不难,我们只需要看当人位于某个点的时候,其四个方向是否有炮塔,这个炮塔是都向人的方向射击,然后再看子弹是否刚好位于这个坐标即可。

而标记的话,vis[x][y][time],对于time时刻,人位于x,y的情况只需要访问一次,这是唯一的
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
#define ads(x) (x<0?-x:x)
int n,m,k,life;
int to[5][2] = {0,1,1,0,0,-1,-1,0,0,0};//四个方向与停止不动的走法
int map[105][105];
bool vis[105][105][1005];

struct period
{
    char c;
    int t,v;
} s[105][105];

struct node
{
    int x,y,step;
};

int check(int x,int y)
{
    if(x<0 || x>n || y<0 || y>m)
        return 1;
    return 0;
}

void bfs()
{
    node a,next;
    queue<node> Q;
    int i,j,flag,dis,tem;
    a.x = a.y = a.step = 0;
    Q.push(a);
    vis[0][0][0] = true;
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
          if(a.step>life)
            break;
        if(a.x == n && a.y == m)
        {
            printf("%d\n",a.step);
            return ;
        }
        for(i = 0; i<5; i++)
        {
            next = a;
            next.x+=to[i][0];
            next.y+=to[i][1];
            next.step++;
            if(check(next.x,next.y)) continue;
            if(!s[next.x][next.y].t && !vis[next.x][next.y][next.step] && next.step<=life)//在符合条件的情况下,枚举四个方向
            {
                flag = 1;
                for(j = next.x-1; j>=0; j--)//当位于这点,我们往北向寻找是否有朝南方向射击的炮台
                {
                    if(s[j][next.y].t && s[j][next.y].c == 'S')//找到第一个炮台,且这个炮台是朝南射击的
                    {
                        dis = next.x-j;//看炮台与人的距离
                        if(dis%s[j][next.y].v) break;//因为不需要看子弹中途的点,子弹每一秒跑v,距离是dis,dis不能整除v的话,那么子弹是不可能停在这个点的
                        tem = next.step-dis/s[j][next.y].v;//人走的时间减去第一个子弹飞行到这个位置所需的时间
                        if(tem<0) break;//为负数就是第一个子弹都没有经过这个点,那么人绝对安全
                        if(tem%s[j][next.y].t==0)//看间隔,能整除,那么就是后续有子弹刚好到这个点,人死定了
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][next.y].t)//找到炮台但不是朝南射击,那么这个炮台会当下后面所有子弹,所以北方向安全我们不需要再找
                        break;
                }
                if(!flag)//这个方向都死定了,后面也就不需要看了
                    continue;
                //其他方向也是一样的道理,就不注释了
                for(j = next.x+1; j<=n; j++)
                {
                    if(s[j][next.y].t && s[j][next.y].c == 'N')
                    {
                        dis = j-next.x;
                        if(dis%s[j][next.y].v) break;
                        tem = next.step-dis/s[j][next.y].v;
                        if(tem<0) break;
                        if(tem%s[j][next.y].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][next.y].t)
                        break;
                }
                if(!flag)
                    continue;
                for(j = next.y-1; j>=0; j--)
                {
                    if(s[next.x][j].t && s[next.x][j].c == 'E')
                    {
                        dis = next.y-j;
                        if(dis%s[next.x][j].v) break;
                        tem = next.step-dis/s[next.x][j].v;
                        if(tem<0) break;
                        if(tem%s[next.x][j].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[next.x][j].t)
                        break;
                }
                if(!flag)
                    continue;
                for(j = next.y+1; j<=m; j++)
                {
                    if(s[next.x][j].t && s[next.x][j].c == 'W')
                    {
                        dis = j-next.y;
                        if(dis%s[next.x][j].v) break;
                        tem = next.step-dis/s[next.x][j].v;
                        if(tem<0) break;
                        if(tem%s[next.x][j].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[next.x][j].t)
                        break;
                }
                if(!flag)
                    continue;
                vis[next.x][next.y][next.step] = true;
                Q.push(next);
            }
        }
    }
    printf("Bad luck!\n");
}

int main()
{
    int i,j,x,y,t,v;
    char str[3];
    while(~scanf("%d%d%d%d",&n,&m,&k,&life))
    {
        memset(s,0,sizeof(s));
        memset(vis,false,sizeof(vis));
        for(i = 0; i<k; i++)
        {
            scanf("%s%d%d%d%d",str,&t,&v,&x,&y);
            s[x][y].v = v;
            s[x][y].t = t;
            s[x][y].c = str[0];
        }
        bfs();
    }

    return 0;
}
时间: 2024-12-11 05:47:27

HDU3533:Escape(BFS)的相关文章

HDU 3533 Escape (BFS + 预处理)

Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 541    Accepted Submission(s): 141 Problem Description The students of the HEU are maneuvering for their military training. The red army

HDU 3533 Escape BFS搜索

题意:懒得说了 分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的 #include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <queue> #include

【搜索】 HDU 3533 Escape BFS 预处理

要从0,0 点 跑到m,n点  路上会有k个堡垒发射子弹,有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒 可以上下左右或者站着不动 每步都需要消耗能量  一共有eng个能量 先预处理出地图 用三维数组表示mp[x][y][time] time表示该时间的地图上储存不能走的点 然后就是普通BFS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <s

【HDU - 3533】Escape(bfs)

Escape  Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到达终点要求: 1.人不能到达炮塔所在的坐标2.炮塔会挡住子弹3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死4.人可以选择停止不动 Input 对于每个测试用例,第一行有四个整数,m.n.k和d (2<=m, n<=100, 0<=k<=100, m

ACM: Gym 101047E Escape from Ayutthaya - BFS

Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to it

CodeForces Gym 101047E Escape from Ayutthaya BFS

Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 101047E Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to its collaps

[Swust OJ 1023]--Escape(带点其他状态的BFS)

解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Description BH is in a maze,the maze is a matrix,he wants to escape! Input The input consists of multiple test cases. For each case,the first line contains 2 inte

FZU 2196 Escape(BFS预处理+BFS搜索)

Problem 2196 Escape Accept: 123    Submit: 678 Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 小明进入地下迷宫寻找宝藏,找到宝藏后却发生地震,迷宫各处产生岩浆,小明急忙向出口处逃跑.如果丢下宝藏,小明就能迅速离开迷宫,但小明并不想轻易放弃自己的辛苦所得.所以他急忙联系当程序员的朋友你(当然是用手机联系),并告诉你他所面临的情况,希望你能告诉他是否能成功带着宝藏

BFS(判断状态) HDOJ 3533 Escape

题目传送门 题意:一个人从(0, 0)逃往(n, m),地图上有朝某个方向开炮的炮台,问最少逃脱步数 分析:主要在状态是否OK,当t时刻走到(x,y),炮台是否刚好打中,因为只能是整数,所以用整除判断.题意不清楚,有些坑点. #include <bits/stdc++.h> using namespace std; const int N = 1e2 + 5; struct Point { int dir, t, v; //N 1 E 2 S 3 W 4 }p[N][N]; struct No