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给出如上N*M的矩阵,表示一个监狱布局:其中o表示路,#表示墙,a表示需要营救的成员位置,r和x分别表示营救者和警卫,数量不唯一。r每次的移动范围包括东南西北四个方向的相邻位置,要求给出所有r到a的最短路径。遇到路可以继续走,遇到墙走不通,遇到警卫则可以干掉他继续走,但干掉警卫需要花费走一格的时间。如果不存在这样的一条路径则输出:"Poor ANGEL has to stay in the prison all his life."这个题由于有多个救援者,只有一个被救者,所以把被救者当作bfs的起点,救援者当作bfs的终点,求出离被救援者最近的救援者就可以了#include<iostream> #include<queue> #include<cstdio> using namespace std; char tu[205][205]; const char start='a'; const char endd='r'; const char monster='x'; int dir[4][2]={-1,0,0,1,1,0,0,-1}; int n,m; struct friendd { int x,y,time; friendd(int A,int B,int C=0){x=A;y=B;time=C;} bool operator <(const friendd &m)const{return time>m.time;} bool operator == (const friendd &m)const{return time==m.time;} }p(0,0); int bfs() { int next_x,next_y; priority_queue<friendd>dui; dui.push(p); while(!dui.empty()) { friendd f=dui.top();dui.pop();if(tu[f.x][f.y]=='#')continue; if(tu[f.x][f.y]==endd){return f.time;} if(tu[f.x][f.y]==monster){f.time=f.time+1;} tu[f.x][f.y]='#'; for(int i=0;i<4;i++) { next_x=f.x+dir[i][0]; next_y=f.y+dir[i][1]; if(next_x<0||next_x>=n||next_y<0||next_y>=m)continue; if(tu[next_x][next_y]=='#')continue; friendd o(next_x,next_y,f.time+1);dui.push(o); } }return 0; } int main() { while(scanf("%d %d",&n,&m)!=EOF) {for(int i=0;i<n;i++) {scanf("%s",tu[i]); for(int j=0;j<m;j++) if(tu[i][j]==start)p=friendd(i,j); }int t=bfs();if(t!=0)printf("%d\n",t); else cout<<"Poor ANGEL has to stay in the prison all his life.\n"; } }