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
中文
题目描述
Angel被MOLIGPY抓住,并被监禁在监狱中。这个监狱被划分成一个n*m(n,m<=200)的矩阵,里面有围墙、道路和监狱警卫。
Angel的朋友们想把她救出来。他们的首要任务就是要找到Angel所在的位置。当有一个警卫在某个格子中,他们必须杀了他(或她?)才能移动到这个格子。假设他们可以上,下,左,右移动需要耗费1单位的时间,杀死一名警卫也耗费1单位的时间。假设他们足够强大,足以杀死所有的警卫。
你必须计算出找到Angel的最少时间。当然他们只能在相邻的格子里上,下,左,右移动。
输入格式
第一行包含两个整数n和m。
接下来的n行,每行m个字符。“#”表示墙壁,“x”表示警卫,“.”代表道路,“a”代表Angel,“r”代表Angel的朋友们。
输出格式
一个整数,表示最少需要的时间。如果这样的时间不存在,应该输出“Poor ANGEL has to stay in the prison all his life.”。
样例输入1
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
样例输出1
13
题目来源
hdu1242
是BFS,但要用优先队列每次选时间最小的出队,要重载运算符,
r有多个从a开始搜r
#include<bits/stdc++.h>
using namespace std;
int n,m,a[202][202],ax,ay,rx,ry,xj[10]={0,-1,0,1},yj[10]={-1,0,1,0};
string s;
struct node{
int x,y,t;
};
bool operator < (const node &x, const node &y){
return x.t>y.t;
}
priority_queue<node>q;
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s;
s=" "+s;
for(int j=1;j<=m;j++){
switch(s[j]){
case 'a':ax=i,ay=j;break;
case 'x':a[i][j]=2;break;
case '.':a[i][j]=1;break;
case 'r':a[i][j]=-1;break;
}
}
}
q.push((node){ax,ay,0});
// a[ax][ay]=0;
while(!q.empty()){
node now=q.top();
int xx=now.x,yy=now.y,tt=now.t;
q.pop();
// cout<<xx<<" "<<yy<<" "<<tt<<"\n";
for(int i=0;i<4;i++){
int xxx=xx+xj[i],yyy=yy+yj[i];
if(xxx>0&&xxx<=n&&yyy<=m&&yyy>0&&a[xxx][yyy]!=0){
if(a[xxx][yyy]==-1){
cout<<tt+1<<"\n";
return 0;
}
q.push((node){xxx,yyy,tt+a[xxx][yyy]});
a[xxx][yyy]=0;
}
}
}
cout<<"Poor ANGEL has to stay in the prison all his life.\n";
}
/*
7 8
#.#####.
#.a.x.r.
#...#...
.......#
#...##..
.#......
........
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#..#...
........
*/