HDU 1242 Rescue(BFS+優先隊列)

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21483    Accepted Submission(s): 7660


Problem Description
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
 

Author
CHEN, Xue
 


AC代碼及題解:

/*
author: tpbluesky
time:   2015年8月10日18:13:49 
題解:	廣搜加優先隊列,此題有兩個坑點,要用優先隊列是因爲遇到x時時間+2,會破壞隊列裏元素的有序性 
		1.angle的朋友可能不止一個,故可以從angle開始廣搜
		2.angle可能不存在,所以最後還得判斷一下 
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#define inf 0x3f3f3f3f
#define eps 1e-8
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
const int maxn = 205;
int n, m, vis[maxn][maxn], ans; 
char mp[maxn][maxn];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};

struct node
{
	int x, y ,step;
	int operator<(const node &n) const
	{
		return step>n.step;
	}
};

bool isok(int x,int y)
{
	if(x <= 0 || y <= 0 || x > n || y > m || mp[x][y] == '#' || vis[x][y])
		return false;
	return  true;
}

int bfs(int x,int y)
{
	node s;
	memset(vis,0,sizeof(vis));
	s.x = x, s.y = y,s.step= 0;
	priority_queue<node> pq;
	pq.push(s);
	vis[x][y] = 1;
	while(!pq.empty())
	{
		node tp = pq.top();
		pq.pop();
		if(mp[tp.x][tp.y] == 'r')
		{
			return tp.step;
		}
		for(int i = 0;i < 4;++i)
		{
			node temp;
			temp.x = tp.x+dx[i], temp.y = tp.y+dy[i], temp.step = tp.step+1;
			if(isok(temp.x,temp.y))
			{
				vis[temp.x][temp.y] = 1;
				if(mp[temp.x][temp.y] == 'x')
					temp.step++;
				pq.push(temp);
			}
		}
	}
	return -1;
}

int main()
{
	while(scanf("%d%d",&n,&m) == 2)
	{
		int flag = 1, ans = inf;
		for(int i = 1;i <= n;++i)
			scanf("%s",mp[i]+1);
		for(int i = 1;i <= n;++i)
			for(int j = 1;j <= m;++j)
				if(mp[i][j] == 'a')
				{
					int ans = bfs(i,j);
					flag = 0;
					printf(ans == -1?"Poor ANGEL has to stay in the prison all his life.\n":"%d\n",ans);
					i = n+1;
					break; 
				}
		if(flag)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
	}
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章