Rescue--优先队列加bfs

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

思路:倒着跑全图求最短路优先队列要先跑否则这个样例你过不去

10 10
##########
##########
##########
##axx#####
##.#xxx###
##.#xxx###
##.#rxx###
##...xx###
##########
##########

正确是7

错误13

ac代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1010;
const int mod = 100;
int T,n,m,minn=INF;
int book[250][250];
char map1[250][250];
struct node{
	int x,y,step;
	node(){
	}
	node(int xx,int yy,int sstep):x(xx),y(yy),step(sstep){
	}
	bool operator < (const node & b)const
	{
		return b.step<step;
	}
};
const int rr[4][2]={1,0,0,1,-1,0,0,-1};
int xstart,ystart,xend,yend;
int bfs(int x,int y,int step)
{
	priority_queue<node>A;
	A.push(node(x,y,step));
	book[x][y] = 1;
	while(!A.empty())
	{
		node u = A.top();
		A.pop();
			if(map1[u.x][u.y] == 'r')//当找到时更新最大值
			{
				if(u.step<minn){
					minn=u.step;
				}
				//return ss;
			}
		for(int i = 0;i < 4;++i)
		{
			int xx = u.x + rr[i][0];
			int yy = u.y + rr[i][1];
			int ss = u.step + 1;
			/*if(xx == xend && yy == yend)
			{
				if(ss<minn){
					minn=ss;
				}
				//return ss;
			}*/
			if(xx < 1 || yy < 1 || xx > n || yy >m || book[xx][yy] == 1 || map1[xx][yy] == '#')
			{
				continue;
			}
			if(map1[xx][yy] == 'x')
			{
				ss++;
			}
			book[xx][yy] = 1;
			A.push(node(xx,yy,ss));
		}
		
	}
	return minn;
}//套bfs模板
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		string s;
		minn=INF;
		memset(map1,0,sizeof(map1));
		memset(book,0,sizeof(book));
		for(int i = 1;i <= n;++i)
		{
			cin>>s;
			for(int j = 1;j <= m;++j)
			{
				map1[i][j] = s[j-1];
				if(map1[i][j] == 'a')
				{
					xstart = i;
					ystart = j;
				}
				/*if(map1[i][j] == 'a')
				{
					xend = i;
					yend = j;
				}*/
			}
		}
//		for(int i=1;i<=n;++i)
//		{
//			for(int j=1;j<=m;++j)
//			{
//				printf("%c",map1[i][j]);
//			}
//			printf("\n");
//		}
	
		int ans = bfs(xstart,ystart,0);
		if(ans == INF)
		{
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		}
		else{
			printf("%d\n",ans);
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值