Ignatius and the Princess I HDU - 1026(BFS+优先队列)

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
思路:因为时间优先,而且每到一个格子花费的时间是不一样的(因为有的格子要打怪花费时间),所以我们单纯的用队列不太好处理了,因此用到了优先队列。按照花费时间有小到大排序,到达终点肯定是最小的,返回就可以了。关键是记录路径,可以开一个数组记录每一个格子走的方向,我是采用的是在结构体中带一个string字符串记录路径,因为这个数据量比较小,所以是可行的,并且不需要递归耗时比较少。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e2+10;
char s[maxx][maxx];
int vis[maxx][maxx];
int d[][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node{
	int x,y,v;
	string s;
	node(int a,int b,int c,string d)
	{
		x=a,y=b,v=c,s=d;
	}
	bool operator<(const node &a)const{
		return v>a.v;
	}
};
int n,m;

inline node bfs()
{
	priority_queue<node> q;
	vis[0][0]=1;
	q.push(node(0,0,0,""));
	string t;
	while(q.size())
	{
		node u=q.top();
		q.pop();
		if(u.x==n-1&&u.y==m-1) return u;
		for(int i=0;i<4;i++)
		{
			int tx=u.x+d[i][0];
			int ty=u.y+d[i][1];
			if(tx<0||tx>=n||ty<0||ty>=m||s[tx][ty]=='X'||vis[tx][ty]) continue;
			t=u.s;
			if(i==0) t+='D';
			else if(i==1) t+='R';
			else if(i==2) t+='U';
			else t+='L';
			vis[tx][ty]=1;
			if(s[tx][ty]>='0'&&s[tx][ty]<='9') q.push(node(tx,ty,u.v+1+s[tx][ty]-'0',t));
			else q.push(node(tx,ty,u.v+1,t));
		}
	}
	return node(0,0,0,"给爷爬"); 
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++) scanf("%s",s[i]);
		node a=bfs();
		if(a.s=="给爷爬") printf("God please help our poor hero.\n");
		else
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",a.v);
			int x=0,y=0;
			int num=1;
			for(int i=0;i<a.s.length();i++)
			{
				if(a.s[i]=='L') printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,x,y-1),y-=1;
				else if(a.s[i]=='R') printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,x,y+1),y+=1;
				else if(a.s[i]=='U') printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,x-1,y),x-=1;
				else printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,x+1,y),x+=1;
				if(s[x][y]>='0'&&s[x][y]<='9')
				{
					int zz=s[x][y]-'0';
					while(zz) printf("%ds:FIGHT AT (%d,%d)\n",num++,x,y),zz--;
				}
			}	
		}
		printf("FINISH\n");
	}
	return 0;
}

努力加油a啊,(o)/~

发布了652 篇原创文章 · 获赞 101 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105311970