# hdoj 3533 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.

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

Source
2010 ACM-ICPC Multi-University Training Contest（10）——Host by HEU

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include <queue>

using namespace std;
const int maxn = 101;

struct castle
{
int d, t, v, x, y;
};

struct node
{
int x, y, t;
};
castle c[maxn];
int maze[maxn][maxn];
bool vis[maxn][maxn][maxn * 10];
bool state[maxn][maxn][maxn * 10];

int dir[5][2] = { {-1,0}, {1,0}, {0,1}, {0,-1},{0 ,0} };
char D;
int n, m, k, d;

bool judge(int x,int y)
{
if (maze[x][y] || x<0 || x > n || y < 0 || y > m)
return false;
return true;
}

void init()
{
memset(state, 0, sizeof(state));
for (int i = 0; i < k; i++)
{
for (int j = 0; j <= d; j += c[i].t)
{
int cnt = 1;
while (true)
{
int x = c[i].x + dir[c[i].d][0] * cnt;
int y = c[i].y + dir[c[i].d][1] * cnt;
if (judge(x, y))
{
int bullet = j + cnt / c[i].v;
if (cnt % c[i].v == 0)
{
state[x][y][bullet] = 1;
}
}
else
{
break;
}
cnt++;
}
}
}
}

int bfs()
{
queue<node> Q;
memset(vis, 0, sizeof(vis));

node tmp;
tmp.x = tmp.y = tmp.t = 0;
vis[0][0][0] = 1;
Q.push(tmp);
while (!Q.empty())
{
tmp = Q.front();
Q.pop();
if (tmp.t > d)
{
return -1;
}
if (tmp.x == n && tmp.y == m)
{
return tmp.t;
}
for (int i = 0; i < 5; i++)
{
node res;
res.x = tmp.x + dir[i][0];
res.y = tmp.y + dir[i][1];
res.t = tmp.t + 1;
if (judge(res.x, res.y) && !vis[res.x][res.y][res.t] && !state[res.x][res.y][res.t])
{
vis[res.x][res.y][res.t] = 1;
Q.push(res);
}
}
}
return -1;
}

int main() {
ios::sync_with_stdio(false);
while (cin >> n >> m >> k >> d)
{
memset(maze, 0, sizeof(maze));
for (int i = 0; i < k; i++)
{
//	getchar();
cin >> D >> c[i].t >> c[i].v >> c[i].x >> c[i].y;
switch (D)
{
case 'N' :
c[i].d = 0; break;
case 'S':
c[i].d = 1; break;
case 'E' :
c[i].d = 2; break;
case 'W':
c[i].d = 3; break;
default:
c[i].d = -1;
}
maze[c[i].x][c[i].y] = 1;
}

init();
int ans = bfs();
if (ans == -1)
{
cout << "Bad luck!" << endl;
}
else
{
cout << ans << endl;
}

}
return 0;
}



bfs先确定人物行走时的变化状态包括什么，在不同的时刻，人的位置，地图中炮弹的状态，人的能量值都有不同，所以状态表示为三维，即位置加上时间即可。

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

红鲤鱼遇绿鲤鱼

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

04-23 33

11-23 27
03-11 109
12-13 4026
12-14 1581
04-11 8999
09-17 8449
08-23 2011
04-06
07-02 54