您的位置:首页 > 产品设计 > UI/UE

HDOJ 1242 Rescue(BFS+优先对列)

2015-03-24 20:56 288 查看

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): 18735    Accepted Submission(s): 6700


[align=left]Problem Description[/align]
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.)

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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."

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

13

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<queue>
#define MAX 210
#include<algorithm>
using namespace std;
struct Road
{
int x,y,num;
friend bool operator < (Road a,Road b)//优先队列
{
return a.num>b.num;//以num较小优先
}
} s,q,p;
char graph[MAX][MAX];//存储图
int num[MAX][MAX],//从r开始到当前位置所用的最小时间
dri[4][2]={//方向
{-1,0},{1,0},{0,-1},{0,1}
};
bool judge[MAX][MAX];//判断是否已经走过
int main()
{
int r,c,i,j,X,Y;
int x_1,y_1,/*天使的朋友坐标*/x_2,y_2/*天使的坐标*/;
while(~scanf("%d %d",&r,&c))
{
memset(num,0,sizeof(num));
for(i=0;i<r;i++)
{
getchar();
for(j=0;j<c;j++)
{
scanf("%c",&graph[i][j]);
if(graph[i][j]=='r')
{
x_1=i;y_1=j;//
}
if(graph[i][j]=='a')
{
x_2=i;y_2=j;//
}
if(graph[i][j]=='x')
num[i][j]=1;//如果是警卫
}
}

memset(judge,false,sizeof(judge));
s.x=x_1;s.y=y_1;
num[x_1][y_1]=1;
s.num=num[x_1][y_1];
judge[x_1][y_1]=true;
priority_queue<Road>Q;
Q.push(s);
while(!Q.empty())
{
q=Q.top();
Q.pop();
for(i=0;i<4;i++)
{
X=q.x+dri[i][0];
Y=q.y+dri[i][1];
if(X<0||X>=r||Y<0||Y>=c||graph[X][Y]=='#'||judge[X][Y]) continue;
else
{
judge[X][Y]=true;
num[X][Y]+=(num[q.x][q.y]+1);
p.num=num[X][Y];
p.x=X;p.y=Y;
Q.push(p);
}
}
}

if(num[x_2][y_2]!=0)
printf("%d\n",num[x_2][y_2]-1);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: