HDU 1026 Ignatius and the Princess I

题目链接:HDU 1026

Description

The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.

Input

The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

Output

For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

Sample Input

5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.

Sample Output

It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH

题意

输入一个迷宫,迷宫由路,墙和怪兽组成,每个怪兽的血量不同,可以消耗等同于怪兽血量的时间来通过这块地面,问能否逃离迷宫,并把最短路径输出

题解:

迷宫问题的变形,带权BFS,本题有连个难点,一个是走迷宫时打怪兽打哪个或者绕路不打,第二个就是输出完整的路径。本题依然是BFS,刚开始还不信,其实只要把队列改成优先队列就可以了,默认的格子设为怪兽血量为0,然后宽搜时怪兽入队,优先弹出血量最小的(因为要尽快走出嘛,优先走没怪的,其实也是一种贪心思想),然后在每一步走的时候记录每个点由上一个点走哪个方向走来,用1,2,3,4表示,在输出时用到递归,先从终点开始一步步往回走,但是在调用递归函数之后输出,这样就达到了输出从起点到终点路径的效果,这种方法也可以方便地解决本来是正序,但是可以反向输出的问题。

代码

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;

struct node
{
	int x, y;
	int step;
	friend bool operator<(node n1, node n2)
	{
		return n2.step<n1.step;
	}
};
int dir[4][2] = { 0,1, 1,0, 0,-1, -1,0 };
int map[111][111];
int flag[111][111];
int blood[111][111];
int n, m;

int judge(int x, int y)
{
	if (x<0 || x >= n || y<0 || y >= m)	return 1;
	if (map[x][y] == -1)				return 1;
	return 0;
}
int BFS()
{
	priority_queue<node>q;
	node cur, next;
	int i;
	cur.x = 0;
	cur.y = 0;
	cur.step = 0;
	map[0][0] = -1;

	q.push(cur);
	while (!q.empty())
	{
		cur = q.top();
		q.pop();
		if (cur.x == n - 1 && cur.y == m - 1)	return cur.step;
		for (i = 0; i<4; i++)
		{
			next.x = cur.x + dir[i][0];
			next.y = cur.y + dir[i][1];

			if (judge(next.x, next.y))	continue;
			next.step = cur.step + 1 + map[next.x][next.y];
			flag[next.x][next.y] = i + 1;
			map[next.x][next.y] = -1;
			q.push(next);
		}
	}
	return -1;
}
int temp;
void P(int x, int y)
{
	int next_x, next_y;
	if (flag[x][y] == 0)	return;
	next_x = x - dir[flag[x][y] - 1][0];
	next_y = y - dir[flag[x][y] - 1][1];
	P(next_x, next_y);
	printf("%ds:(%d,%d)->(%d,%d)
", temp++, next_x, next_y, x, y);

	while (blood[x][y]--)	printf("%ds:FIGHT AT (%d,%d)
", temp++, x, y);
}

int main()
{
	char str[111];
	int i, l;
	int ans;

	while (scanf("%d%d", &n, &m) != -1)
	{
		memset(map, 0, sizeof(map));
		memset(flag, 0, sizeof(flag));
		memset(blood, 0, sizeof(blood));
		for (i = 0; i<n; i++)
		{
			scanf("%s", str);
			for (l = 0; str[l]; l++)
			{
				if (str[l] == '.')		map[i][l] = 0;
				else if (str[l] == 'X')map[i][l] = -1;
				else				map[i][l] = blood[i][l] = str[l] - '0';
			}
		}

		ans = BFS();
		if (ans == -1)	printf("God please help our poor hero.
");
		else
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.
", ans);

			temp = 1;
			P(n - 1, m - 1);
		}
		printf("FINISH
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Titordong/p/9591577.html