HDU3533 - Escape

来源:互联网 发布:端口地址查询位置 编辑:程序博客网 时间:2024/06/07 05:53

D - Escape
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 3533
Appoint description: 

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 10N 1 1 1 1W 1 1 3 2W 2 1 2 44 4 3 10N 1 1 1 1W 1 1 3 2W 1 1 2 4
 

Sample Output

9Bad luck!

从(0,0)走到(n,m),有k个碉堡在不同位置朝固定方向每隔t发射一个子弹,子弹速度v不相同。问能否在d时刻内走到(n,m)

试了几种思路都MLE,结果发现每个时间的子弹分布要用bool存才不会超内存。g数组记录子弹分布

因为人可以选择原地不动,所以vis数组要开三维,每个时间只能访问某个地点一次。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <string>using namespace std;#define N 105#define T 1005int n,m,k,d;int vx[]={0,0,0,1,-1};int vy[]={0,1,-1,0,0};//CEWSNbool vis[N][N][T];bool g[N][N][T];struct node{    int x,y,s;    node(int xx,int yy,int ss):x(xx),y(yy),s(ss){}};struct guard{    int r,t,v,x,y;    guard(){}    guard(int rr,int tt,int vv,int xx,int yy):r(rr),t(tt),v(vv),x(xx),y(yy){}};bool check(int x,int y){    if (x<0 || x>n || y<0 || y>m)        return false;    return true;}guard man[N];void update(guard u){    int r,t,v,x,y;    r=u.r; t=u.t; v=u.v;    x=u.x; y=u.y;    int num=1;    while (1){        int tx=x+vx[r]*v*num;        int ty=y+vy[r]*v*num;        if (!check(tx,ty)) break;        if (g[tx][ty][0]) break;        int flag=0;        int xx=x+vx[r],yy=y+vy[r];        while (xx!=tx || yy!=ty){            if (g[xx][yy][0]){                flag=1;                break;            }            xx+=vx[r];            yy+=vy[r];        }        if (flag) break;        int s=num;        while (s<=d){            g[tx][ty][s]=1;            s+=t;        }        num++;    }}void bfs(){    memset(vis,0,sizeof(vis));    queue<node> q;    q.push(node(0,0,0));    vis[0][0][0]=1;    while (!q.empty()){        node cur=q.front();        q.pop();        int x=cur.x,y=cur.y,s=cur.s;        if (x==n && y==m){            printf("%d\n",s);            return;        }        if (s==d)            continue;        for (int i=0;i<5;i++){            int tx=x+vx[i];            int ty=y+vy[i];            int ts=s+1;            if (!check(tx,ty)) continue;            if (g[tx][ty][0]) continue;            if (g[tx][ty][ts]) continue;            if (vis[tx][ty][ts]) continue;            vis[tx][ty][ts]=1;            q.push(node(tx,ty,ts));        }    }    printf("Bad luck!\n");}int main(){    while (~scanf("%d%d%d%d",&n,&m,&k,&d)){        memset(g,0,sizeof(g));        for (int i=0;i<k;i++){            char st[2];            scanf("%s",st);            int r,t,v,x,y;            scanf("%d%d%d%d",&t,&v,&x,&y);            if (st[0]=='E')                r=1;            if (st[0]=='W')                r=2;            if (st[0]=='S')                r=3;            if (st[0]=='N')                r=4;            man[i]=guard(r,t,v,x,y);            g[x][y][0]=1;        }        for (int i=0;i<k;i++)            update(man[i]);        bfs();    }    return 0;}


0 0
原创粉丝点击