hdoj-1242-Rescue【广搜+优先队列】

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21510 Accepted Submission(s): 7671

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

Source

ZOJ Monthly, October 2003

Recommend

Eddy | We have carefully selected several similar problems for you:
1240 1072 1253 1372 1175

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char MAP[202][202];
bool visit[202][202];
struct node{
	int x,y;
	int flag;
};
int u[4]={1,0,-1,0};
int v[4]={0,1,0,-1};
int cmp(node a,node b){
	return a.flag<b.flag;
}
int BFS(int i,int j){
    queue<node> Q;  //Q要在数组内定义,否则要记得每次用之前将队列清空,因为上次的队列可能会由于没有完全出队而保留上次的数据,其中我在这就出了错!
    node t;
    t.x=i;  t.y=j;
    t.flag=0;
    Q.push(t);
    visit[i][j]=1;
   	while(!Q.empty()){
   		t=Q.front();
   		int tx,ty;
   		tx=t.x;ty=t.y;
   		if(MAP[tx][ty]=='r'){
   			return t.flag;
   		}
   		Q.pop();
   		node temp[4];
   		int p=0,k;
   		for(k=0;k<4;++k){
   			if(MAP[tx+u[k]][ty+v[k]]!='#'&&!visit[tx+u[k]][ty+v[k]]){
   				if(MAP[tx+u[k]][ty+v[k]]=='x'){
   					temp[p].x=tx+u[k];
   					temp[p].y=ty+v[k];
   					visit[temp[p].x][temp[p].y]=1;
   					temp[p].flag=t.flag+2;
   					++p;
   				}
   				else{
   					temp[p].x=tx+u[k];
   					temp[p].y=ty+v[k];
   					visit[temp[p].x][temp[p].y]=1;
   					temp[p].flag=t.flag+1;
   					++p;
   				}
   			//	Q.push(temp); //此处不能 直接入队,因为同等级的 r要比 x耗时短,所以先将 t上下左右的四个点存放到数组中
   			}
   		}
   		sort(temp,temp+p,cmp);  //经过排列后再依次入队
   		for(k=0;k<p;++k){
   			Q.push(temp[k]);
   		}
   	}
   	return -1;
}
int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		int i,j;
		for(i=0;i<=n+1;++i){
			for(j=0;j<=m+1;++j){
				MAP[i][j]='#';
			}
		}
		int ax,ay;
		for(i=1;i<=n;++i){
		   getchar();
			for(j=1;j<=m;++j){
				scanf("%c",&MAP[i][j]);
				if(MAP[i][j]=='a') ax=i,ay=j;
			}
		}
		memset(visit,0,sizeof(visit));
		int res=BFS(ax,ay);
		if(res==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",res);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 03:33:28

hdoj-1242-Rescue【广搜+优先队列】的相关文章

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

hdu 1026 Ignatius and the Princess I 广搜+优先队列+记录路径

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13414    Accepted Submission(s): 4232 Special Judge Problem Description The Princess has been abducted by the BEelzeb

ZOJ 1649 &amp;&amp; HDU 1242 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

hdoj 1242 Rescue (BFS)

Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18962    Accepted Submission(s): 6771 Problem Description Angel was caught by the MOLIGPY!

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

BFS HDOJ 1242 Rescue

题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不会,看个数组模拟队列的BFS看的头晕,现在看起来也不过如此,额,当年开始是从r走到a的,因为数据巨弱才过的,应该要用到优先队列. /************************************************ * Author :Running_Time * Created Ti

HDU1180 诡异的楼梯 广搜 优先队列

Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的. Input 测试数据有多组,每组的表

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!