Escape

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.

InputFor 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.
OutputIf 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!

  1 #include<iostream>
  2 #include<queue>
  3 #include<cstring>
  4 #include<cstdio>
  5 using namespace std;
  6 int n,m,k,life;
  7 int to[5][2]={0,1,1,0,0,-1,-1,0,0,0};//包括一种不走的情况
  8 int map[105][105];//存地图
  9 bool vis[105][105][105];//记录当前的x,y,time
 10 
 11 struct period
 12 {
 13     char c;
 14     int t,v;
 15 }s[105][105];
 16 struct node
 17 {
 18     int x;int y;int step;
 19 };
 20 int check(int x,int y)//检查边界
 21 {
 22     if(x<0||x>n||y<0||y>m)
 23         return 1;
 24     return 0;
 25 }
 26 void bfs()
 27 {
 28     node a,next;
 29     queue<node> Q;
 30     int i,j,flag,dis,num,tem;
 31     a.x=a.y=a.step=0;//(0,0)开始
 32     Q.push(a);
 33     vis[0][0][0]=true;//标记初始位置点
 34     while(!Q.empty())
 35     {
 36         a=Q.front();
 37         Q.pop();
 38         if(a.step>life)
 39             break;
 40         if(a.x==n&&a.y==m)//找到终点
 41         {
 42             printf("%d\n",a.step);
 43             return ;
 44         }
 45         for(i=0;i<5;i++)
 46         {
 47             next=a;//用next备份a
 48             next.x+=to[i][0];
 49             next.y+=to[i][1];
 50             next.step++;
 51             if(check(next.x,next.y)) continue;
 52             if(!s[next.x][next.y].t&&!vis[next.x][next.y][next.step]&&next.step<=life)
 53             {//射击间隔不为0,即这个点没有炮台,没有炮台才能走
 54                 flag=1;//flag初始化为1
 55                 for(j=next.x-1;j>=0;j--)//在每一行找向北找是否有朝向南方向设计的炮台,因为只有像左或者右跑
 56                 {//才会被向南或北的炮台打
 57                     if(s[j][next.y].t&&s[j][next.y].c=='S')
 58                     {
 59                         dis=next.x-j;//计算炮台与人的距离
 60                         if(dis%s[j][next.y].v) break;//不需要计算子弹中途的点,所以直接计算子弹是否能停在此位置
 61                         tem=next.step-dis/s[j][next.y].v;
 62                         //人走的时间减去第一个子弹飞行的时间,计算一下时间差
 63                         if(tem<0)//如果人还没走到这个点
 64                             break;//人一定不会死
 65                         if(tem%s[j][next.y].t==0)//剩下的时间正好能整除子弹的发射间隔,所以人一定会死
 66                         {
 67                             flag=1;
 68                             break;
 69                         }
 70                     }
 71                     if(s[j][next.y].t)
 72                         break;//找到此位置有炮台,则可以挡下所有子弹
 73                 }
 74                 if(!flag)//这个方向死掉了,就不需要判断了
 75                     continue;
 76                 for(j=next.x+1;j<=n;j++)
 77                 {
 78                     if(s[j][next.y].t&&s[next.x][j].c=='N')
 79                     {
 80                         dis=j-next.x;
 81                         if(dis%s[j][next.y].v) break;
 82                         tem=next.step-dis/s[j][next.y].v;
 83                         if(tem<0) break;
 84                         if(tem%s[j][next.y].t==0)
 85                         {
 86                             flag=0;
 87                             break;
 88                         }
 89                     }
 90                     if(s[j][next.y].t)
 91                         break;
 92                 }
 93                 if(!flag)
 94                     continue;
 95                 for(j=next.y-1;j>=0;j--)
 96                 {
 97                     if(s[next.x][j].t&&s[next.x][j].c=='E')
 98                     {
 99                         dis = next.y-j;
100                         if(dis%s[next.x][j].v) break;
101                         tem=next.step-dis/s[next.x][j].v;
102                         if(tem<0)
103                             break;
104                         if(tem%s[next.x][j].t==0)
105                         {
106                             flag=0;
107                             break;
108                         }
109                     }
110                     if(s[next.x][j].t)
111                         break;
112                 }
113                 if(!flag)
114                     continue;
115                 for(j=next.y+1;j<=m;j++)
116                 {
117                     if(s[next.x][j].t&&s[next.x][j].c=='W')
118                     {
119                         dis=j-next.y;
120                         if(dis%s[next.x][j].v) break;
121                         tem= next.step-dis/s[next.x][j].v;
122                         if(tem<0)
123                             break;
124                         if(tem%s[next.x][j].t==0)
125                         {
126                             flag=0;
127                             break;
128                         }
129                     }
130                     if(s[next.x][j].t)
131                         break;
132                 }
133                 if(!flag)
134                     continue;
135                 vis[next.x][next.y][next.step]=true;
136                 Q.push(next);
137             }
138         }
139     }
140     printf("Bad luck!\n");
141 }
142 int main()
143 {
144     int i,j,x,y,t,v;
145     char str;
146     while(~scanf("%d%d%d%d",&n,&m,&k,&life))//k代表炮塔数量,life代表能量值
147     {
148         memset(s,0,sizeof(s));//初始化
149         memset(vis,false,sizeof(vis));//初始化
150         for(i=0;i<k;i++)
151         {
152             scanf("%s%d%d%d%d",&str,&t,&v,&x,&y);//t射击间隔,v子弹速度
153             s[x][y].v=v;
154             s[x][y].t=t;
155             s[x][y].c=str;//字符串独到整数,会自动切换变量
156             //这里str用单独的char型可以代替
157             /*for(int i=0;i<3;i++)
158             cout<<"|"<<str[i]<<"|"<<endl;*/
159         }
160 
161          bfs();
162     }
163    return 0;
164 }

 

posted @ 2020-11-29 20:27  BlackSnow  阅读(78)  评论(0)    收藏  举报