Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21825 Accepted Submission(s): 7776
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.)
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.
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
//hdu-1242rescue(bfs)
//题目大意:天使被关在一个房间里,他的朋友(不止一个)想去救他 ;首先要接近天使,已知每移动一位花费一个
//单位时间,当遇到守卫时,杀死守卫要另外花费一个单位时间(当然,如果可以避开守卫就不用多花费这个时间);
//问接近天使的最短时间是多少?
//解题思路:由于天使的朋友不止一个,所以为了方便,我们以天使为起点,去找理他最近的朋友,这样花费的时间最少;
//其次,就是要用到优先队列,并自定义花费时间少的优先级高,这样每次出队列的都是当前的最短路径所用的时间;
//最终求的的求得就是解禁天使所用的最短时间;
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int vis[210][210];
char map[210][210];
struct node{
int s,e;
int time;
friend bool operator<(node a,node b) //自定义优先级
{ //time值小的优先级高
return a.time>b.time;
}
}temp,a1;
int jude(node temp)
{
if(temp.s<0||temp.s>=n||temp.e<0||temp.e>=m) //判断是否越界;
return 0;
if(map[temp.s][temp.e]=='#'||vis[temp.s][temp.e]==1)//判断是否能走或已走过;
return 0;
return 1;
}
int bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
a1.s=x;
a1.e=y;
a1.time=0;
vis[a1.s][a1.e]=1;
priority_queue<node>q; //定义一个优先队列;
q.push(a1);
while(!q.empty())
{
a1=q.top();
q.pop();
if(map[a1.s][a1.e]=='r') //当找到最近的一个朋友时,返回所有时间;
{
return temp.time;
}
for(int i=0;i<4;i++)
{ //bfs搜索与其最近的朋友;
temp.s=a1.s+dx[i];
temp.e=a1.e+dy[i];
if(jude(temp))
{
vis[temp.s][temp.e]=1;
if(map[temp.s][temp.e]=='x') //当遇到守卫时时间加2;
temp.time=a1.time+2;
else
temp.time=a1.time+1;
q.push(temp);
}
}
}
return -1; //若天使初始就被困住不能移动,返回-1;表示其朋友无法靠近他;
}
int main()
{
int i,j,sx,sy;
int tim;
while(scanf("%d%d",&n,&m)!=EOF)
{
tim=0;
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<m;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='a'){
sx=i;
sy=j;
}
}
}
tim=bfs(sx,sy);
if(tim==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",tim);
}
return 0;
}