天天看点

(HDU3533)Escape-BFSEscape

Escape

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2513    Accepted Submission(s): 737

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.

(HDU3533)Escape-BFSEscape

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!

 题目分析:

题目大意人在精力的限制下能否从(0,0)走到(m,n),若能求最短的时间。炮塔的位置不能走,炮弹打到的位置不能走,用数组保存不同时间炮弹能打到的位置。

代码:忘了看的那个大佬的了,这就不发链接了,链接找不到了。

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;

struct Node {
	int x, y, s;
}start;
//保存堡垒的位置。
struct cannon {
	int r, T, v, x, y;
}numCannon[100];
bool vis[100][100][1000];
bool Bullet[100][100][1000];//将d时间内不同时间点炮弹能打到的位置记录下来
int n, m, k, d;
int dx[] = { 0,0,0,1,-1 }, dy[] = { 0,1,-1,0,0 };

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

void BFS() {
	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;
			if (Bullet[temp.x][temp.y][0])continue;
			if (Bullet[temp.x][temp.y][temp.s])continue;
			vis[temp.x][temp.y][temp.s] = true;
			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];
		}
		//该位置上的炮弹每隔T秒会出现一次
		int t = S;
		while (t <= d)
		{
			Bullet[tx][ty][t] = true;
			t += tem.T;
		}
		S++;
	}
}



int main() {
	while (cin >> n >> m >> k >> d)
	{
		memset(vis, false, sizeof(vis));
		memset(Bullet, false, sizeof(Bullet));

		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++) {
			update(numCannon[i]);
		}
		BFS();
	}
	
	return 0;
}