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
Bad luck!

Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU

中文:

给你一个二维坐标,你起始位置在(0,0),让你走到(m,n),问你最少时间。
坐标系上有的位置有大炮,大炮会在四个方向之一,以固定的t发射炮弹,炮弹的速度为v,表示每秒中v个格子。
初始给你d点能量,时间过一个单位浪费一点,如果在能量耗尽前没走完,你就狗带。

代码:

#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先确定人物行走时的变化状态包括什么,在不同的时刻,人的位置,地图中炮弹的状态,人的能量值都有不同,所以状态表示为三维,即位置加上时间即可。
可以在地图中计算出每个时刻,当前地图炮弹的情况(能剩点内存),也可以提前算好所有时刻炮弹的位置。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页