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

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. "); else printf("%d ",res); } return 0; }



原文地址:https://www.cnblogs.com/lytwajue/p/6890269.html