1. 程式人生 > >HDU 3533 Escape 【bfs】

HDU 3533 Escape 【bfs】

Escape

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


 

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

Recommend

zhouzeyong

題意:

小A要從(0,0)點逃到(m,n)點 。有k個敵人碉堡在座標上。小A最開始有d點能量,每過一秒會耗費一點能量。如果能量耗盡,小A就無法逃出昇天。每個碉堡都會向某個方向(東西南北)發射一枚子彈,速度為v,發射週期為t。但是子彈會被碉堡所擋住。且子彈不會相撞。

子彈只能在整數座標處擊中小A。小A不會在非整數座標下停下(但可以在整數座標停下)。問在不被擊中且能量不耗盡的情況下,最快什麼時候能到達(m,n)點。

要注意:

1:小A不能走到敵人碉堡處。

2:終點可能會有碉堡

3:起點如果有碉堡的話,不影響出發。但是如果起點處有碉堡的話,不能在起點停留。

用vis[x][y][t]陣列來標記

代表在t時刻到達(x,y)點。且這種情況是不能出現兩次的。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
int m,n,k,d,flag;
char c;
int t,v,x,y;
int X[6] = {0,0,1,-1,0};///五個方向是上下左右和停止不動
int Y[6] = {-1,1,0,0,0};
struct Node1
{
    int id;///記錄方向
    int per,speed;
} castle[MAXN][MAXN];
bool vis[MAXN][MAXN][1005];///bool 型變數可節約記憶體
struct Node2
{
    int x,y,t;
};

void init()
{
    M(castle,0);
    M(vis,false);
}

int bfs(int x,int y,int t)
{
    queue<Node2> q;
    Node2 temp;
    temp = {x,y,t};
    vis[0][0][0] = true;
    q.push(temp);
    while(!q.empty())
    {
        int x1 = q.front().x;
        int y1 = q.front().y;
        int t1 = q.front().t;
        q.pop();
        if(t1>d) break;///耗盡能量
        if(x1==m&&y1==n)///到達終點
        {
            return t1;
        }
        for(int i=0; i<5; i++)///遍歷五個方向,包括停止不動
        {
            int xx = x1+X[i];
            int yy = y1+Y[i];
            int tt = t1+1;
            if(xx<0||xx>m||yy<0||yy>n||castle[xx][yy].id !=0||vis[xx][yy][tt]==true||tt>d)///座標不越界,且沒有敵人碉堡,且沒被訪問過,且剩餘能量充足
                continue;
            int flag2 =1;
            for(int j=xx+1; j<=m; j++)///向右邊找
            {
                if(castle[j][yy].id!=0)///找到一個碉堡
                {
                    if(castle[j][yy].id!=1)///碉堡方向開火向左
                    {
                        break;
                    }
                    else
                    {
                        if((j-xx)%castle[j][yy].speed!=0)///是否會出現子彈(整數點)
                        {
                            break;
                        }
                        int temp = (j-xx)/castle[j][yy].speed;///算出子彈到達的時間
                        if(tt-temp<0)break;
                        if((tt-temp)%castle[j][yy].per==0)///會被擊中
                        {
                            flag2 = 0;

                        }
                    }
                    break;
                }

            }
            if(flag2==0)///如果該點會被擊中
            {
                continue;
            }
            for(int j=xx-1; j>=0; j--)///向左找
            {
                if(castle[j][yy].id!=0)
                {
                    if(castle[j][yy].id!=2)
                    {
                        break;
                    }
                    else
                    {
                        if((xx-j)%castle[j][yy].speed!=0)
                        {
                            break;
                        }
                        int temp = (xx-j)/castle[j][yy].speed;
                        if(tt-temp<0)break;
                        if((tt-temp)%castle[j][yy].per==0)
                        {
                            flag2 = 0;
                        }
                    }
                    break;
                }

            }
            if(flag2==0)
            {
                continue;
            }
            for(int j=yy+1; j<=n; j++)///向上找
            {
                if(castle[xx][j].id!=0)
                {
                    if(castle[xx][j].id!=3)
                    {
                        break;
                    }
                    else
                    {
                        if((j-yy)%castle[xx][j].speed!=0)
                        {
                            break;
                        }
                        int temp = (j-yy)/castle[xx][j].speed;
                        if(tt-temp<0)break;
                        if((tt-temp)%castle[xx][j].per==0)
                        {
                            flag2 = 0;
                        }
                    }
                    break;
                }
            }
            if(flag2==0)
            {
                continue;
            }
            for(int j=yy-1; j>=0; j--)///向下找
            {
                if(castle[xx][j].id!=0)
                {
                    if(castle[xx][j].id !=4)
                    {
                        break;
                    }
                    else
                    {
                        if((yy-j)%castle[xx][j].speed!=0)
                        {
                            break;
                        }
                        int temp = (yy-j)/castle[xx][j].speed;
                        if(tt-temp<0)break;
                        if((tt-temp)%castle[xx][j].per==0)
                        {
                            flag2 = 0;
                        }
                    }
                    break;
                }
            }
            if(flag2==0)///如果被擊中,則放棄該點
            {
                continue;
            }
            else
            {
                vis[xx][yy][tt] = true;
                Node2 temp2 = {xx,yy,tt};
                q.push(temp2);
            }

        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d %d %d %d",&m,&n,&k,&d))
    {
        init();
        for(int i=0; i<k; i++)
        {
            getchar();
            scanf("%c %d %d %d %d",&c,&t,&v,&x,&y);///NSWE
            if(c=='N')
            {
                castle[x][y] = {1,t,v};
            }
            else if(c=='S')
            {
                castle[x][y] = {2,t,v};
            }
            else if(c=='W')
            {
                castle[x][y] = {3,t,v};
            }
            else if(c=='E')
            {
                castle[x][y] = {4,t,v};
            }

        }
        int ans = bfs(0,0,0);
        if(ans==-1)
        {
            printf("Bad luck!\n");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
    return 0;
}