HDU - 3533 Escape (BFS)(day_4_H)

Escape

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

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!

Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

Recommend
zhouzeyong | We have carefully selected several similar problems for you: 3535 3527 3528 3529 3530

题目简述:

m*n矩阵,要从(0,0)走到(m,n),图上有堡垒,堡垒会往上下左右中的一个方向射子弹,子弹射在别的堡垒会被阻挡。人不能被射中,不能进堡垒,可以在原地躲子弹。每秒走一格,问能否在给定时间走到目的地。

题目分析:

BFS,预先把子弹出现的位置时间用zd[x][y][time]标记。

代码实现:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
#define pf printf
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define ms(i,j) memset(i,j,sizeof(i))

char c;
int m,n,k,d,t,v,x,y,xt,yt,dd[5][2]= {-1,0,1,0,0,-1,0,1,0,0};
bool vis[105][105][1005],bl[105][105],zd[105][105][1005];
struct sb
{
    int x,y,s;
}tmp,now;
struct bll
{
    int t,v,x,y;
    char c;
}jb[105];
bool ok()
{
    if(tmp.s>d) return false;
    int nmb=abs(tmp.x-m)+abs(tmp.y-n);
    return d-tmp.s>=nmb;
}
void bfs()
{
    memset(vis,false,sizeof(vis));
    queue<sb> line;
    tmp={0,0,0};
    line.push(tmp);
    vis[0][0][0]=true;
    while(!line.empty())
    {
        now=line.front();
        line.pop();
        if(now.x==m&&now.y==n)
        {
            pf("%d\n",now.s);
            return;
        }
        for(int i=0;i<5;i++)
        {
            tmp.x=now.x+dd[i][0];
            tmp.y=now.y+dd[i][1];
            tmp.s=now.s+1;
            if(tmp.x<=m&&tmp.x>=0&&tmp.y<=n&&tmp.y>=0&&ok()&&!bl[tmp.x][tmp.y]&&!vis[tmp.x][tmp.y][tmp.s]&&!zd[tmp.x][tmp.y][tmp.s])
            {
                line.push(tmp);
                vis[tmp.x][tmp.y][tmp.s]=1;
            }
        }
    }
    pf("Bad luck!\n");
}
int main()
{
    while(~scanf("%d%d%d%d",&m,&n,&k,&d))
    {
        memset(bl,false,sizeof(bl));
        memset(zd,false,sizeof(zd));
        getchar();
        for(int i=0;i<k;i++)
        {
            scanf("%c",&jb[i].c);
            scanf("%d%d%d%d",&jb[i].t,&jb[i].v,&jb[i].x,&jb[i].y);
            getchar();
            bl[jb[i].x][jb[i].y]=true;
        }
        for(int l=0;l<k;l++)
        {
            c=jb[l].c;
            t=jb[l].t;
            v=jb[l].v;
            x=jb[l].x;
            y=jb[l].y;
            if(c=='N')
                for(int i=1;; i++)
                {
                    xt=i*(-1)+x;
                    yt=y;
                    if(xt<0||xt>m||yt<0||yt>n)
                        break;
                    if(bl[xt][yt])
                        break;
                    if(i%v==0)
                        for(int j=i/v; j<=d; j+=t)
                            zd[xt][yt][j]=true;
                }
            if(c=='S')
                for(int i=1;; i++)
                {
                    xt=i+x;
                    yt=y;
                    if(xt<0||xt>m||yt<0||yt>n)
                        break;
                    if(bl[xt][yt])
                        break;
                    if(i%v==0)
                        for(int j=i/v; j<=d; j+=t)
                            zd[xt][yt][j]=true;
                }
            if(c=='W')
                for(int i=1;; i++)
                {
                    xt=x;
                    yt=y+i*(-1);
                    if(xt<0||xt>m||yt<0||yt>n)
                        break;
                    if(bl[xt][yt])
                        break;
                    if(i%v==0)
                        for(int j=i/v; j<=d; j+=t)
                            zd[xt][yt][j]=true;
                }
            if(c=='E')
                for(int i=1;; i++)
                {
                    xt=x;
                    yt=y+i;
                    if(xt<0||xt>m||yt<0||yt>n)
                        break;
                    if(bl[xt][yt])
                        break;
                    if(i%v==0)
                        for(int j=i/v; j<=d; j+=t)
                            zd[xt][yt][j]=true;
                }
        }
        bfs();
    }
}

附上快乐的做题历程:
在这里插入图片描述