HDU1242- Rescue (广度优先搜索的经典模板)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. 

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards. 

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.) 

Input

First line contains two integers stand for N and M. 

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

Process to the end of the file. 

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13

这是广度优先搜索的经典模板题(bfs)。宽度优先搜索算法(又称广度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型。Dijkstra单源最短路径算法和Prim最小生成树算法都采用了和宽度优先搜索类似的思想。其别名又叫BFS,属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果。换句话说,它并不考虑结果的可能位置,彻底地搜索整张图,直到找到结果为止。

实例以及解析

广度优先搜索算法 
  假设从a顶点开始访问,a先入队。此时队列非空,取出队头元素a,由于b,c和a直接相邻且未被访问过,于是依次访问b,c,并且b,c依次入队。队列非空,取出队头元素b,依次访问与b相邻且未被访问的顶点d,e,并且将d,e入队(注意:a与b也邻接,但是a已经访问过,就不会再访问了)。此时队列非空,取出队头元素c,访问与c邻接且未被访问过的顶点f,g,并且将f,g入队。此时,取出队头元素d,但与d邻接且为被访问的顶点为空,故不再进行任何操作,继续取出对头元素e,将h入队….当最后取出队头元素h后,队列为空,从而循环自动跳出,遍历的结果为abcdefgh。

走迷宫也是同理,只不过每一个节点的子节点都有4个(因为当前点有上下左右四个方向)。

AC代码为:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXN 200+10
using namespace std;
int n,m;
char str[MAXN][MAXN];
int vis[MAXN][MAXN];
int to[4][2]={-1,0,1,0,0,1,0,-1};
struct node{
	int x;
	int y;
	int step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
};

void BFS(int stx,int sty,int edx,int edy)
{
	memset(vis,0,sizeof(vis));
	priority_queue que;
	node a,next;
	int ans=0;
	a.x=stx;
	a.y=sty;
	a.step=0;
	que.push(a);
	vis[stx][sty]=1;
	while(!que.empty())
	{
		a=que.top();
		que.pop();
		if(a.x==edx&&a.y==edy)
		{
			ans=a.step;
			break;
		}	
		for(int i=0;i<4;i++)
		{
			next=a;
			next.x+=to[i][0];
			next.y+=to[i][1];
			if(next.x<0||next.y<0||next.x>=n||next.y>=m||vis[next.x][next.y]||str[next.x][next.y]=='#')
				continue;
			next.step++;
			if(str[next.x][next.y]=='x')
				next.step++;
			vis[next.x][next.y]=1;
			que.push(next);
//			if(vis[next.x][next.y]>=next.step)
//			{
//				vis[next.x][next.y]=next.step;
//				que.push(next);
//			}
		}
	}
	if(ans)
		printf("%d\n",ans);
	else
		puts("Poor ANGEL has to stay in the prison all his life.");
}

int main()
{
	int stx,sty,edx,edy;
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i

 

你可能感兴趣的:(bfs)