Fellow me on GitHub

HDU3533(KB2-D)

Escape

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1407    Accepted Submission(s): 392


Problem 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 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!
 

 

Source

 
明明是考虑到挡子弹的,判断的时候却没有写,浪费了好多时间。。。
  1 //2017-03-09
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include  <queue>
  6 
  7 using namespace std;
  8 
  9 const int N = 105;
 10 int grid[N][N];
 11 bool vis[N][N][1005];
 12 int n, m, k, d, ans;
 13 int dx[5] = {0, 1, 0, -1, 0};
 14 int dy[5] = {1, 0, -1, 0, 0};
 15 struct castle
 16 {
 17     char dir;
 18     int t, v;
 19 }cas[N];
 20 struct node
 21 {
 22     int x, y, step;
 23     void setNode(short x, short y, short step)
 24     {
 25         this->x = x;
 26         this->y = y;
 27         this->step = step;
 28     }
 29 };
 30 
 31 bool judge(int x, int y, int Time)
 32 {
 33     for(int i = y-1; i >= 0; i--)
 34     {
 35         if(grid[x][i]){
 36             if(cas[grid[x][i]].dir == 'E' && (y-i)%cas[grid[x][i]].v == 0 && (Time-(y-i)/cas[grid[x][i]].v)>=0 && (Time-(y-i)/cas[grid[x][i]].v)%cas[grid[x][i]].t == 0)
 37               return false;
 38         }
 39         if(grid[x][i])break;
 40     }
 41     for(int i = y+1; i <= m; i++){
 42         if(grid[x][i])
 43           if(cas[grid[x][i]].dir == 'W' && (i-y)%cas[grid[x][i]].v == 0 && (Time-(i-y)/cas[grid[x][i]].v)>=0 && (Time-(i-y)/cas[grid[x][i]].v)%cas[grid[x][i]].t == 0)
 44             return false;
 45         if(grid[x][i])break;
 46     }
 47 
 48     for(int i = x-1; i >= 0; i--){
 49       if(grid[i][y])
 50         if(cas[grid[i][y]].dir == 'S' && (x-i)%cas[grid[i][y]].v == 0 && (Time-(x-i)/cas[grid[i][y]].v)>=0 && (Time-(x-i)/cas[grid[i][y]].v)%cas[grid[i][y]].t == 0)
 51           return false;
 52       if(grid[i][y])break;
 53     }
 54     for(int i = x+1; i <= n; i++){
 55       if(grid[i][y])
 56         if(cas[grid[i][y]].dir == 'N' && (i-x)%cas[grid[i][y]].v == 0 && (Time-(i-x)/cas[grid[i][y]].v)>=0 && (Time-(i-x)/cas[grid[i][y]].v)%cas[grid[i][y]].t == 0)
 57           return false;
 58       if(grid[i][y])break;
 59     }
 60     return true;
 61 }
 62 
 63 bool bfs()
 64 {
 65     node tmp;
 66     queue<node> q;
 67     memset(vis, 0, sizeof(vis));
 68     vis[0][0][0] = 1;
 69     tmp.setNode(0, 0, 0);
 70     q.push(tmp);
 71     int x, y, nx, ny, step;
 72     if(grid[n][m])return false;
 73     while(!q.empty())
 74     {
 75         x = q.front().x;
 76         y = q.front().y;
 77         step = q.front().step;
 78         if(step>d)return false;
 79         q.pop();
 80         for(int i = 0; i < 5; i++)
 81         {
 82             nx = x+dx[i];
 83             ny = y+dy[i];
 84             if(nx>=0&&nx<=n&&ny>=0&&ny<=m&&!grid[nx][ny]&&!vis[nx][ny][step+1]&&judge(nx, ny, step+1)&&step+1<=d)
 85             {
 86                 if(nx==n&&ny==m){
 87                     ans = step+1;
 88                     return true;
 89                 }
 90                 vis[nx][ny][step+1] = 1;
 91                 tmp.setNode(nx, ny, step+1);
 92                 q.push(tmp);
 93             }
 94         }
 95     }
 96     return false;
 97 }
 98 
 99 int main()
100 {
101     while(scanf("%d%d%d%d", &n, &m, &k, &d)!=EOF)
102     {
103         int x, y;
104         char ch[3];
105         memset(grid, 0, sizeof(grid));
106         for(int i = 1; i <= k; i++)
107         {
108             scanf("%s%d%d%d%d", ch, &cas[i].t, &cas[i].v, &x, &y);
109             cas[i].dir = ch[0];
110             grid[x][y] = i;
111         }
112         if(bfs())printf("%d\n", ans);
113         else printf("Bad luck!\n");
114     }
115 
116     return 0;
117 }

 

posted @ 2017-03-09 21:02  Penn000  阅读(202)  评论(0编辑  收藏  举报