Problem Description
Angel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.
Angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or his?) 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 had 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-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 the 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 have to stay in the prison all he life."
Sample Input
7 8
#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
is a classic deep search question, did 1180 questions again to do this, is the second a AH.
Next Test instructions:
Angels are locked in a prison, a for Angels, R for Angels, X for Guards, #是墙. R can kill so the guards, killing a guard takes 1 units of time, and moving one step also requires a unit of time. Ask, Angel's friend R shortest how long it takes to save angel A.
If the Angels cannot be rescued, output: Poor ANGEL has to stay in the prison all his life.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace STD;structnode{intX,y,t;};intN,m;Char Map[ About][ About];intd[ About][ About];intAx,ay,rx,ry;node ft;intmx[]={1,0,-1,0};intmy[]={0,-1,0,1};intJudgeintXintY) {if(x<0|| x>=n| | y<0|| Y>=M) {return 0; }if(Map[x] [y]==' # '){return 0; }if(D[x][y]) {return 0; }return 1;}voidBFS () { queue<node>Q Ft.x=rx; Ft.y=ry; ft.t=0; Q.push (FT); d[rx][ry]=1; while(!q.empty ()) {Ft=q.front (); Q.pop ();//printf ("%d,%d,%d\n", ft.x,ft.y,ft.t); intX=ft.x;intY=ft.y;if(X==ax&&y==ay) {printf("%d\n", ft.t);return; } for(intI=0;i<4; i++) {x=ft.x+mx[i]; Y=ft.y+my[i];if(!judge (x, y)) {Continue; } node NT;if(Map[x] [y]=='. '||Map[x] [y]==' A ') {nt.x=x; Nt.y=y; nt.t=ft.t+1; d[x][y]=1; Q.push (NT); }Else if(Map[x] [y]==' x ') {nt.x=x; Nt.y=y; nt.t=ft.t+2; d[x][y]=1; Q.push (NT); } } }printf("Poor ANGEL have to stay in the prison all his life.\n");}intMain () { while(~scanf("%d%d", &n,&m)) {memset(d,0,sizeof(d)); for(intI=0; i<n;i++) {scanf('%s ',Map[i]); for(intj=0; j<m;j++) {if(MapI [j]==' A ') {ax=i,ay=j; }if(MapI [j]==' R ') {rx=i,ry=j; }}} BFS (); }return 0;}
Hdoj/hdu 1242 Rescue (classic BFS deep Search)