您的位置:首页 > 其它

hdoj 1026 Ignatius and the Princess I 【BFS + 优先队列 + stack路径记录】 【经典题目】

2015-06-23 13:40 423 查看


Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14123 Accepted Submission(s): 4453

Special Judge


Problem 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+优先队列,主要是路径记录。
思路:结构体除了存储x, y, step(分别是横坐标,纵坐标和走到当前的最少步数),需要存储prex,prey来记录前一坐标。然后结构体设置一个二维数组path[x][y]记录(x,y)对应的结构体节点。在用队列存储结构体节点时,要更新path[][]和prex以及prey。找到后直接从path[n-1][m-1]节点向前找即可,用栈存储比较方便,注意输出路径时要时刻判断栈是否为空。

15ms:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
struct Node
{
	int x, y, step, prex, prey;
	friend bool operator < (Node a, Node b)
	{
		return a.step > b.step;
	}
};
Node path[110][110];
char map[110][110];//地图 
int vis[110][110];//标记是否查询 
int n, m;//行 列 
void getmap()
{
	for(int i = 0; i < n; i++)
	{
		getchar();
		for(int j = 0; j < m; j++)
		scanf("%c", &map[i][j]); 
	}
}
bool judge(int x, int y)
{
	return x >= 0 && x < n && y >= 0 && y < m && map[x][y] != 'X' && !vis[x][y];
}
void findpath(int ex, int ey)
{
	stack<Node> rec;
	Node now, next;
	now = path[ex][ey];
	rec.push(now);
	while(1)
	{
		now = path[now.prex][now.prey];
		rec.push(now);
		if(now.x == 0 && now.y == 0)
		break;
	}
	int time = 1;
	printf("It takes %d seconds to reach the target position, let me show you the way.\n", path[n-1][m-1].step);
	while(!rec.empty())
	{
		now = rec.top();
		rec.pop(); 
		if(!rec.empty())//栈不为空 不能少这一步!!! 
		next = rec.top();//下一位置 
		if(map[now.x][now.y] == '.')
		{
			if(rec.empty())//最后一步为. 栈空直接跳出 
			break; 
			printf("%ds:", time++);
			printf("(%d,%d)->(%d,%d)\n", now.x, now.y, next.x, next.y);
		}
		else //最后一步为数字 不能直接跳出  
		{
			int need = map[now.x][now.y] - '0';
			while(need--)
			{
				printf("%ds:", time++);
				printf("FIGHT AT (%d,%d)\n", now.x, now.y);
			}
			if(rec.empty())//FIGHT 过后需要跳出 
			break;
			printf("%ds:", time++);
			printf("(%d,%d)->(%d,%d)\n", now.x, now.y, next.x, next.y);
		}
	}
	printf("FINISH\n");
}
void BFS()
{
	int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
	priority_queue<Node> Q;
	Node now, next;
	now.x = now.y = now.step = 0;
	Q.push(now);
	bool exist = false;
	memset(vis, 0, sizeof(vis));
	while(!Q.empty())
	{
		now = Q.top();
		Q.pop();
		if(now.x == n-1 && now.y == m-1)
		{
			path[n-1][m-1] = now;
			exist = true;
			break;
		}
		for(int k = 0; k < 4; k++)
		{
			next.x = now.x + move[k][0];
			next.y = now.y + move[k][1];
			if(judge(next.x, next.y))
			{
				vis[next.x][next.y] = 1;
				next.prex = now.x;
				next.prey = now.y;
				if(map[next.x][next.y] >= '1' && map[next.x][next.y] <= '9')
				next.step = now.step + map[next.x][next.y] - '0' + 1;
				else
				next.step = now.step + 1; 
				Q.push(next);
				path[next.x][next.y] = next;
			} 
		}
	}
	if(exist)
	findpath(n-1, m-1);
	else
	{
		printf("God please help our poor hero.\n");
		printf("FINISH\n");
	}
} 
int main()
{
	while(scanf("%d%d", &n, &m) != EOF)
	{
		getmap();
		BFS();
	} 
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: