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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值