首页 > 代码库 > HDU-3533-Escape(BFS)

HDU-3533-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!
 

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

思路:普通BFS,要看清楚题目啊,碉堡是会把子弹挡住的,因为这个WA了一整天。。。

#include <stdio.h>

struct S{
int x,y,time;
}que[1000000],t;

struct{
char c;
int time,v;
}mp[101][101];

int nxt[5][2]={{0,1},{1,0},{0,-1},{-1,0},{0,0}};
bool vis[101][101][1001];

int main()
{
    int n,m,k,d,i,j,time,v,x,y,dis;
    char s[5];

    while(~scanf("%d%d%d%d",&n,&m,&k,&d))
    {
        for(i=0;i<=n;i++) for(j=0;j<=m;j++) mp[i][j].time=0;

        while(k--)
        {
            scanf("%s%d%d%d%d",s,&time,&v,&x,&y);

            mp[x][y].time=time;
            mp[x][y].v=v;
            mp[x][y].c=s[0];
        }

        for(i=0;i<=n;i++) for(j=0;j<=m;j++) for(k=0;k<=d;k++) vis[i][j][k]=0;

        int top=0,bottom=1;

        que[0].x=0;
        que[0].y=0;
        que[0].time=0;

        while(top<bottom)
        {
            t=que[top];

            if(t.x==n && t.y==m)
            {
                printf("%d\n",t.time);

                break;
            }

            t.time++;

            for(i=0;i<5;i++)
            {
                t.x+=nxt[i][0];
                t.y+=nxt[i][1];

                if(t.x>=0 && t.x<=n && t.y>=0 && t.y<=m && !mp[t.x][t.y].time && !vis[t.x][t.y][t.time] && t.time<=d)
                {
                    bool flag=1;

                    for(j=t.x-1;j>=0;j--)
                    {
                        if(mp[j][t.y].time && mp[j][t.y].c=='S')
                        {
                            dis=t.x-j;

                            if(dis%mp[j][t.y].v) break;

                            time=t.time-dis/mp[j][t.y].v;

                            if(time<0) break;

                            if(time%mp[j][t.y].time==0)
                            {
                                flag=0;

                                break;
                            }
                        }

                        if(mp[j][t.y].time) break;
                    }

                    if(!flag)
                    {
                        t.x-=nxt[i][0];
                        t.y-=nxt[i][1];

                        continue;
                    }

                    for(j=t.x+1;j<=n;j++)
                    {
                        if(mp[j][t.y].time && mp[j][t.y].c=='N')
                        {
                            dis=j-t.x;

                            if(dis%mp[j][t.y].v) break;

                            time=t.time-dis/mp[j][t.y].v;

                            if(time<0) break;

                            if(time%mp[j][t.y].time==0)
                            {
                                flag=0;

                                break;
                            }
                        }

                        if(mp[j][t.y].time) break;
                    }


                    if(!flag)
                    {
                        t.x-=nxt[i][0];
                        t.y-=nxt[i][1];

                        continue;
                    }

                    for(j=t.y-1;j>=0;j--)
                    {
                        if(mp[t.x][j].time && mp[t.x][j].c=='E')
                        {
                            dis=t.y-j;

                            if(dis%mp[t.x][j].v) break;

                            time=t.time-dis/mp[t.x][j].v;

                            if(time<0) break;

                            if(time%mp[t.x][j].time==0)
                            {
                                flag=0;

                                break;
                            }
                        }

                        if(mp[t.x][j].time) break;
                    }

                    if(!flag)
                    {
                        t.x-=nxt[i][0];
                        t.y-=nxt[i][1];

                        continue;
                    }

                    for(j=t.y+1;j<=m;j++)
                    {
                        if(mp[t.x][j].time && mp[t.x][j].c=='W')
                        {
                            dis=j-t.y;

                            if(dis%mp[t.x][j].v) break;

                            time=t.time-dis/mp[t.x][j].v;

                            if(time<0) break;

                            if(time%mp[t.x][j].time==0)
                            {
                                flag=0;

                                break;
                            }
                        }

                        if(mp[t.x][j].time) break;
                    }

                    if(!flag)
                    {
                        t.x-=nxt[i][0];
                        t.y-=nxt[i][1];

                        continue;
                    }

                    vis[t.x][t.y][t.time]=1;

                    que[bottom++]=t;
                }

                t.x-=nxt[i][0];
                t.y-=nxt[i][1];
            }

            top++;
        }

        if(top==bottom) puts("Bad luck!");
    }
}

HDU-3533-Escape(BFS)