Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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+优先队列。到某些点的开销不是1,所以每次扩展需要总开销小的先出队。问题在于如何记录路径?反向搜索正向输出,将每个点的父亲节点保存在结构体中。注意,由于搜索是从终点开始,而终点是可能有怪物的,所以入队前要处理一下。

AC代码:

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, m;
int cost[110][110];
struct Map
{
	char val;
	int prex, prey;
}map[110][110];
const int dx[] = {0, 0, 1, -1},
			dy[] = {1, -1, 0, 0};
struct node
{
	int x, y, step;
	bool operator < (const node b) const
	{
		return step > b.step;
	}
};
priority_queue <node> q;
bool input()
{
	if (scanf("%d %d\n", &n, &m) == 2)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
				scanf("%c", &map[i][j].val);
			getchar();
		}
		return 1;
	}
	return 0;
}
void bfs()
{
	int ans = 0;
	node start;
	while (!q.empty())
		q.pop();
	memset(cost, 0, sizeof(cost));
	start.x = n - 1;
	start.y = m - 1;
	if (map[n - 1][m - 1].val >= '0' && map[n - 1][m - 1].val <= '9')
		start.step = cost[n - 1][m - 1] = map[n - 1][m - 1].val - '0';
	else
		start.step = 0;
	q.push(start);
	while (!q.empty())
	{
		node u = q.top();
		q.pop();
		if (u.x == 0 && u.y == 0)
		{
			ans = u.step;
			break;
		}
		node next;
		for (int i = 0; i < 4; i++)
		{
			next.x = u.x + dx[i];
			next.y = u.y + dy[i];
			if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m || map[next.x][next.y].val == 'X')
				continue;
			if (map[next.x][next.y].val == '.')
				next.step = u.step + 1;
			else
			{
				cost[next.x][next.y] = map[next.x][next.y].val - '0';
				next.step = u.step + 1 + cost[next.x][next.y];
			}
			map[next.x][next.y].prex = u.x;
			map[next.x][next.y].prey = u.y;
			map[next.x][next.y].val = 'X';
			q.push(next);
		}
	}
	int nowx = 0, nowy = 0, newx, newy;
	if (ans)
	{
		printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
		for (int i = 1; i <= ans; i++)
		{
			newx = map[nowx][nowy].prex;
			newy = map[nowx][nowy].prey;
			printf("%ds:(%d,%d)->(%d,%d)\n", i, nowx, nowy, newx, newy);
			for (int j = 0; j < cost[newx][newy]; j++)
				printf("%ds:FIGHT AT (%d,%d)\n", ++i, newx, newy);
			nowx = newx;
			nowy = newy;
		}
	}
	else
		printf("God please help our poor hero.\n");
	printf("FINISH\n");
}
int main()
{
	while (input())
		bfs();
	return 0;
}

 

分类: ACM/ICPC

发表评论

电子邮件地址不会被公开。 必填项已用*标注