Escape(HDU - 3533)

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!

題意
小A從(0,0)出發要在d秒內到達(n,m),有以下的限制條件
1)有k個炮臺,不能走,且其會每t[i]秒發射一顆v[i] m/s的炮彈
2)小A不能和炮彈在恰好整數秒相遇,否則會陣亡
3)炮彈會一直移動,直到遇到炮臺或出界
4)小A可以原地停留,同時也會消耗時間

思路
預處理出1~d秒有炮彈的位置shoot[x][y][t]=1(則在第t秒的(x,y)有炮彈。用visit[x][y][t]代表在第t秒訪問的(x,y)的狀態,然後常規bfs即可

#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;

int l,r,d,m;
char c[105];
int x[105],y[105],v[105],t[105];
char s[105][105];
int X[5]= {1,-1,0,0,0};
int Y[5]= {0,0,1,-1,0};
bool visit[105][105][1005];
bool shoot[101][101][1005];

struct node
{
    int x,y,step;
    node():step(0){}
    node(int a,int b,int d):x(a),y(b),step(d){}
} S,e,temp;

void getpos(char tar,int &nx,int &ny)
{
    if(tar=='S') nx=X[0],ny=Y[0];
    if(tar=='N') nx=X[1],ny=Y[1];
    if(tar=='E') nx=X[2],ny=Y[2];
    if(tar=='W') nx=X[3],ny=Y[3];
}

void getshoot()///預處理
{
    int nx,ny;
    for(int i=1; i<=m; i++)
        cin>>c[i]>>t[i]>>v[i]>>x[i]>>y[i],s[x[i]][y[i]]='#';
    memset(shoot,0,sizeof(shoot));
    for(int i=1; i<=m; i++)  ///枚舉堡壘
    {
        getpos(c[i],nx,ny);  ///nx,ny是射擊方向
        int sx=x[i],sy=y[i]; ///堡壘位置
        for(int k=0; k<=d; k+=t[i]) ///模擬第k個子彈發射的時間
        {
            for(int j=1;;j++) ///j爲移動的距離
            {
                int px=sx+nx*j,py=sy+ny*j;
                if(px<0||px>l||py>r||py<0||s[px][py]=='#')///碰到堡壘就停下
                    break;
                if(j%v[i]==0)///當前位置子彈可以到達,到達時間爲j/v[i]
                    shoot[px][py][k+j/v[i]]=true;
            }
        }
    }
}

bool check(int x,int y,int step)
{
    if(s[x][y]=='#'||x<0||x>l)  return false;
    if(y>r||y<0||visit[x][y][step]) return false;
    if(shoot[x][y][step])  return false;
    return true;
}

int solve()
{
    visit[S.x][S.y][S.step]=true;
    queue<node> q;
    q.push(S);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        if(temp.step>d)  continue;///沒能量了
        if(temp.x==e.x&&temp.y==e.y)  return temp.step;
        for(int i=0; i<5; i++)
        {
            int  nx=temp.x+X[i] , ny=temp.y+Y[i];
            if(!check(nx,ny,temp.step+1)) continue;
            visit[nx][ny][temp.step+1]=true;
            q.push({nx,ny,temp.step+1});
        }
    }
    return -1;
}

int main()
{
    while(cin>>l>>r>>m>>d)
    {
        memset(visit,0,sizeof(visit));
        for(int i=0; i<=l; i++)
            for(int j=0; j<=r; j++)
                s[i][j]='.';
        getshoot();///預處理出每一秒有炮彈的位置
        S.x=0,S.y=0;///從(0,0)出發
        e.x=l,e.y=r;
        int ans=solve();
        if(ans==-1) cout<<"Bad luck!"<<endl;///-1忘了判
        else  cout<<ans<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章