Hduoj1026【广搜+优先队列】

/*Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12735    Accepted Submission(s): 4032
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
 

Author
Ignatius.L
*/ 
#include
#include
#include
struct point
{
	int di, loc;
}p[4], q[10010];
int cmp(const void *a, const void *b)
{
	return (*(struct point *)a).di - (*(struct point *)b).di;
}
int  dis[110][110], fa[110][110], n, m, dy[4] = {0, 0, -1, 1}, dx[4] = {-1, 1, 0, 0}; 
char map[110][110];
void bfs()
{
	memset(dis, 0, sizeof(dis));
	int u, x ,  y, front = 0, rear = 0;
	map[0][0] = 'X';
	q[rear].di = 0; 
	q[rear++].loc = 0;//初始位置 
	dis[0][0] = 0;
	while(front < rear)//开始遍历 
	{
		u = q[front++].loc;
		x = u / m;
		y = u % m;
		p[0].di = 1000000;
		p[1].di = 1000000;
		p[2].di = 1000000;
		p[3].di = 1000000;
		for(int d = 0; d < 4; d++)//求出相邻的几个点的距离 
		{
			int nx = x+dx[d], ny = y+dy[d];//四方遍历 
			if(nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != 'X' )//未访问 边界内非墙 
			{
				dis[nx][ny] = dis[x][y] + 1;
				if(map[nx][ny] >= 48 && map[nx][ny] <= 57)
				dis[nx][ny] +=  map[nx][ny] - 48;
				map[nx][ny] = 'X';   //标记  当作墙 
				p[d].di = dis[nx][ny];//保存距离 
				p[d].loc = nx*m+ny;//保存位置 
				fa[nx][ny] = u;//保存父节点 
			} 
		}
		qsort(p, 4, sizeof(p[0]), cmp);//将相邻的点排序 后加入队列  优先队列
		for(int t = 0; t < 4; t++)
		{
			if(p[t].di != 1000000)//存在位置 
			{
				int  ok = 1; 
				for(int i = front; i < rear; i++)
				{
					if(p[t].di < q[i].di)//如果找到比该节点距离大的插入队列 
					{
						for(int j = rear; j > i; j--)//整体后移 
						{
							q[j].di = q[j-1].di;
							q[j].loc = q[j-1].loc;
						}
						q[i].di = p[t].di;//插入
						q[i].loc = p[t].loc;
						ok = 0; 
						rear++;//长度加一 
						break;
					}
				}
				if(ok)//如果没找到插入队尾 
				{
					q[rear].di = p[t].di;
					q[rear++].loc = p[t].loc;
				} 
			}	 
		}
	}
}
int main()
{
	while(scanf("%d%d", &n, &m) != EOF)
	{
		for(int i = 0; i < n; i++)
			scanf("%s", map[i]);
		bfs();
		if(!dis[n-1][m-1])//如果终点距离为0 
		{
			printf("God please help our poor hero.\n");
		}
		else
		{
			int path[10010], i = 1, x = n-1, y = m-1;
			path[0] = (n-1)*m+m-1;//终点 
			printf("It takes %d seconds to reach the target position, let me show you the way.\n", dis[n-1][m-1]);
			while(fa[x][y])//倒置存储 
			{
				path[i++] = fa[x][y];
				int nx = fa[x][y] / m, ny = fa[x][y] % m;
				x = nx;
				y = ny;
			}
			path[i] = 0;
			int k = i;
			while(k)//逆向输出 
			{
				if(dis[path[k-1]/m][path[k-1]%m] - dis[path[k]/m][path[k]%m] == 1)//前后两个点的距离 = 1 
				{
					printf("%ds:(%d,%d)->(%d,%d)\n", dis[path[k-1]/m][path[k-1]%m], path[k]/m,path[k]%m,path[k-1]/m,path[k-1]%m);
					k--;
				}
				else
				{
					int time = dis[path[k]/m][path[k]%m]+1;
					printf("%ds:(%d,%d)->(%d,%d)\n", time, path[k]/m,path[k]%m,path[k-1]/m,path[k-1]%m);//若不是先输出后打架 
					time++;
					while(time <= dis[path[k-1]/m][path[k-1]%m])
					{
						printf("%ds:FIGHT AT (%d,%d)\n", time, path[k-1]/m, path[k-1]%m);
						time++;
					}
					k--;
				}
			}
		}
		printf("FINISH\n");
	}
	return 0;
}

题意:给出一个迷宫,起点为左上角,终点为右下角,途中有数字怪兽,若遇到则到达终点的时间需加上打怪兽的时间,即数字,问是否能到达终点,若能打印时间最短的路径。

思路:首先要用广搜,其次为了求出最短路径,需要用到优先队列,保证每次遍历时每个点求出的时间是最小的。

体会:本题的输出是个大坑,若不注意很容易wa。

你可能感兴趣的:(搜索)