HDU3533 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. 

输入:

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. 

输出:

If Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.

样例:

分析:预处理BFS,坑特别多,简直恶心!(/‵Д′)/~ ╧╧

1.人不能经过碉堡;

2.敌军碉堡可能建到我军基地?!!!

3.子弹碰到碉堡就没了,这里预处理时要注意,碉堡可能在子弹的非击杀点位置,也可能在一秒时子弹出现位置的前面(也就是靠近射出子弹的碉堡)

4.人拥有的能量相当于最大时间

5.人可以禁止不动

  1 #include<iostream>
  2 #include<sstream>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<string>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<functional>
  9 #include<iomanip>
 10 #include<numeric>
 11 #include<cmath>
 12 #include<queue>
 13 #include<vector>
 14 #include<set>
 15 #include<cctype>
 16 #define PI acos(-1.0)
 17 const int INF = 0x3f3f3f3f;
 18 const int NINF = -INF - 1;
 19 typedef long long ll;
 20 using namespace std;
 21 int m, n, k, d;
 22 struct node
 23 {
 24     char dir;
 25     int x, y;
 26     int t, v;
 27 }cas[105];
 28 bool vis[105][105][1005], used[105][105][1005];
 29 bool maze[105][105];
 30 struct path
 31 {
 32     int x, y;
 33     int time;
 34 };
 35 int dx[5] = {1, 0, 0, 0, -1}, dy[5] = {0, 1, 0, -1, 0};
 36 void ini()
 37 {
 38     for (int i = 0; i < k; ++i)
 39     {
 40         if (cas[i].dir == 'N')
 41         {
 42             int flag = 0;
 43             for (int j = cas[i].x - 1; j >= 0; --j)
 44             {
 45                 if (maze[j][cas[i].y])
 46                 {
 47                     flag = j;
 48                     break;
 49                 }
 50             }
 51             for (int j = 1; j <= d; j += cas[i].t)
 52             {
 53                 int s = j;
 54                 for (int g = cas[i].x - cas[i].v; g >= flag; g -= cas[i].v)
 55                     vis[g][cas[i].y][s++] = true;
 56             }
 57         }
 58         else if (cas[i].dir == 'W')
 59         {
 60             int flag = 0;
 61             for (int j = cas[i].y - 1; j >= 0; --j)
 62             {
 63                 if (maze[cas[i].x][j])
 64                 {
 65                     flag = j;
 66                     break;
 67                 }
 68             }
 69             for (int j = 1; j <= d; j += cas[i].t)
 70             {
 71                 int s = j;
 72                 for (int g = cas[i].y - cas[i].v; g >= flag; g -= cas[i].v)
 73                     vis[cas[i].x][g][s++] = true;
 74             }
 75         }
 76         else if (cas[i].dir == 'E')
 77         {
 78             int flag = 0;
 79             for (int j = cas[i].y + 1; j <= n; ++j)
 80             {
 81                 if (maze[cas[i].x][j])
 82                 {
 83                     flag = j;
 84                     break;
 85                 }
 86             }
 87             for (int j = 1; j <= d; j += cas[i].t)
 88             {
 89                 int s = j;
 90                 for (int g = cas[i].y + cas[i].v; g <= flag; g += cas[i].v)
 91                     vis[cas[i].x][g][s++] = true;
 92             }
 93         }
 94         else if (cas[i].dir == 'S')
 95         {
 96             int flag = 0;
 97             for (int j = cas[i].x + 1; j <= m; ++j)
 98             {
 99                 if (maze[j][cas[i].y])
100                 {
101                     flag = j;
102                     break;
103                 }
104             }
105             for (int j = 1; j <= d; j += cas[i].t)
106             {
107                 int s = j;
108                 for (int g = cas[i].x + cas[i].v; g <= flag; g += cas[i].v)
109                     vis[g][cas[i].y][s++] = true;
110             }
111         }
112     }
113 }
114 void bfs()
115 {
116     queue<path> q;
117     q.push(path{0, 0, 0});
118     memset(used, false, sizeof(used));
119     used[0][0][0] = true;
120     while (q.size())
121     {
122         path temp = q.front();
123         q.pop();
124         if (temp.time >= d) break;
125         if (temp.x == m && temp.y == n)
126         {
127             cout << temp.time << endl;
128             return;
129         }
130         for (int i = 0; i < 5; ++i)
131         {
132             int nx = temp.x + dx[i], ny = temp.y + dy[i];
133             int nt = temp.time + 1;
134             if (nx >= 0 && nx <= m && ny >= 0 && ny <= n && !vis[nx][ny][nt] && !maze[nx][ny] && !used[nx][ny][nt])
135             {
136                 used[nx][ny][nt] = true;
137                 q.push(path{nx, ny, nt});
138             }
139         }
140     }
141     cout << "Bad luck!" << endl;
142 }
143 int main()
144 {
145     while (cin >> m >> n >> k >> d)
146     {
147         memset(vis, false, sizeof(vis));
148         memset(maze, false, sizeof(maze));
149         for (int i = 0; i < k; ++i)
150         {
151             cin >> cas[i].dir >> cas[i].t >> cas[i].v >> cas[i].x >> cas[i].y;
152             //cout << cas[i].dir << ' ' << cas[i].t << cas[i].v << cas[i].x << cas[i].y << endl;
153             maze[cas[i].x][cas[i].y] = true;
154         }
155         /*for (int i = 0; i <= m; ++i)
156         {
157             for (int j = 0; j <= n; ++j)
158                 cout << maze[i][j] << ' ';
159             cout << endl;
160         }
161         cout << endl;*/
162         if (maze[m][n])
163         {
164             cout << "Bad luck!" << endl;
165             continue;
166         }
167         ini();
168         /*for (int i = 0; i <= m; ++i)
169         {
170             for (int j = 0; j <= n; ++j)
171                 cout << vis[i][j][2] << ' ';
172             cout << endl;
173         }*/
174         bfs();
175     }
176     return 0;
177 }

 

posted @ 2019-06-14 20:07  Veasky  阅读(396)  评论(0编辑  收藏  举报