HDU - 3533----Escape(BFS+预处理)

来源:互联网 发布:入门级电吉他推荐 知乎 编辑:程序博客网 时间:2024/05/17 09:10

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 Input4 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 4Sample Output9Bad luck!

题目大意:你要从(0,0)走到(n,m),然后有k个炮台,坐标(x,y),以速度v,时间间隔t发射子弹。
你不能被打中,也不能经过炮台的位置,炮塔会挡住子弹 ,你有d的时间,问能不能走到(n,m)。

思路:注意,人可以不动就是有五种方向,不是四种,先预处理每时刻每位置的子弹情况,然后就BFS即可,代码有详细的注解。

#include<iostream>#include<cstring>#include<queue>#include<cstdio> using namespace std;  //这里的坐标x,y是指位于x行y列,不是数学上的(x,y)点,一定要注意这点!!! struct node{    int x,y,s;//人的位置,经过的秒数 }start;struct cannon{//炮塔    int r,T,v,x,y;//方向,周期,速度,坐标  }numCannon[100];//炮塔数int n,m,k,d;  int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};//中东西南北五个方向 bool vis[100][100][1000];//T时刻此位置人是否走过 bool Bullet[100][100][1000];//T时刻该位置是否有子弹 bool check(int x,int y){//没越界true越界false      if(x<0 || x>n || y<0 || y>m){        return false;      }    return true;  }  void BFS(void){        queue<node> Q;     start.x = start.y = start.s = 0;    vis[start.x][start.y][start.s] = true;      Q.push(start);      while(!Q.empty()){          node tem=Q.front();          Q.pop();          if(tem.x==n && tem.y==m){//到达出口             cout<<tem.s<<endl;              return;          }          if(tem.s == d){//能量消耗完             break;        }        for(int i=0 ; i<5 ; i++){              node temp = tem;            temp.x += dx[i];            temp.y += dy[i];            temp.s = tem.s+1;             if(!check(temp.x,temp.y)){//越界                 continue;              }            //这个剪枝很重要,因为人可以在一个地方停            //所以要避免死循环导致内存超出             if(vis[temp.x][temp.y][temp.s]){//已走过                continue;            }else{                vis[temp.x][temp.y][temp.s] = true;            }            if(Bullet[temp.x][temp.y][0]){//该位置是炮塔                 continue;               }              if(Bullet[temp.x][temp.y][temp.s]){//该时刻此位置有子弹                 continue;              }              Q.push(temp);          }      }    cout<<"Bad luck!"<<endl;    return ;  }void update(cannon tem){//得到一个炮塔上的子弹在每个时刻的信息     int S = 1;//经过的秒数     while(1){        int tx = tem.x+dx[tem.r]*tem.v*S;//每过一秒的子弹位置          int ty = tem.y+dy[tem.r]*tem.v*S;        if(!check(tx,ty)){//越界             break;        }          if(Bullet[tx][ty][0]){//这条子弹道上子弹的位置有炮台             return ;          }        int xx=tem.x+dx[tem.r],yy=tem.y+dy[tem.r];//炮台的位置        //检测从初始炮台的位置到此时刻子弹的位置之间是否有其他炮台         while(xx!=tx || yy!=ty){             if(Bullet[xx][yy][0]){//有其他炮台之后子弹打不过去                 return;             }             xx += dx[tem.r];//每次移动一步              yy += dy[tem.r];          }          int t = S;          while(t <= d){//该子弹位置每隔T时刻就有子弹,直到人的能量耗尽             Bullet[tx][ty][t] = true;              t += tem.T;          }          S++;      }  }  int main(void){      while(scanf("%d%d%d%d",&n,&m,&k,&d)!=EOF){          memset(Bullet,false,sizeof(Bullet));        memset(vis,false,sizeof(vis));          for(int i=0 ; i<k ; i++){              char ch;            switch(cin>>ch,ch){                case 'E':numCannon[i].r = 1;break;                case 'W':numCannon[i].r = 2;break;                 case 'S':numCannon[i].r = 3;break;                 case 'N':numCannon[i].r = 4;break;              }            cin>>numCannon[i].T>>numCannon[i].v>>numCannon[i].x>>numCannon[i].y;              Bullet[numCannon[i].x][numCannon[i].y][0] = true;//此处有炮塔         }          for(int i=0 ; i<k ; i++){//预处理T时刻该位置是否有子弹              update(numCannon[i]);          }        BFS();      }      return 0;  }  
0 0
原创粉丝点击