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
#include
#include
#include
#include
using namespace std;
int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
int x, y, step;
bool operator <(const node & t) const
{
return step>t.step;
}
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
void bfs() {
node now, tmp;
int i,xx,yy;
priority_queue q;
now.x = sx, now.y = sy, now.step = 0;
map[sx][sy] = '#';
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
// cout<=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
if(map[xx][yy]=='r') {
cout<>map[i][j];
if(map[i][j]=='a')
sx=i,sy=j;
}
flag = false;
bfs();
if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
#include
#include
#include
#include
using namespace std;
int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
int x, y, step;
bool operator <(const node & t) const
{
return step>t.step;
}
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
void bfs() {
node now, tmp;
int i,xx,yy;
priority_queue q;
now.x = sx, now.y = sy, now.step = 0;
map[sx][sy] = '#';
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
// cout<=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
if(map[xx][yy]=='r') {
cout<>map[i][j];
if(map[i][j]=='a')
sx=i,sy=j;
}
flag = false;
bfs();
if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
#include
#include
#include
#include
using namespace std;
char map[205][205];
int vis[205][205];
int m,n;
int d[4][2]= {0,-1,0,1,-1,0,1,0};
struct node
{
int x,y;
int time;
friend bool operator<(const node &a,const node &b)//运算符重载对优先队列里的小于号进行的重载
{
return a.time>b.time;//时间小的先出队
}
};
int bfs(int x,int y)
{
node st,nt;
priority_queue q;
st.x=x;
st.y=y;
st.time=-1;
q.push(st);
vis[st.x][st.y]=1;
while(!q.empty())
{
st=q.top();
q.pop();
if(map[st.x][st.y]=='r')
return st.time;
for(int i=0; i<4; i++)
{
nt.x=st.x+d[i][0];
nt.y=st.y+d[i][1];
if(!vis[nt.x][nt.y] && nt.x>=0 && nt.x=0 && nt.y