RescueTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21510 Accepted Submission (s): 7671
Problem Descriptionangel 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.)
Inputfirst 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.
Outputfor 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
Authorchen, Xue
Sourcezoj Monthly, October 2003
Recommendeddy | We have carefully selected several similar problems for you:1240 1072 1253 1372 1175
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include < Algorithm>using namespace Std;char map[202][202];bool visit[202][202];struct node{int x,y;int flag;}; int u[4]={1,0,-1,0};int V[4]={0,1,0,-1};int CMP (node A,node b) {return a.flag<b.flag;} int BFS (int i,int j) {queue<node> Q; Q to define within the array, otherwise remember to empty the queue before each use, because the last queue might have left the last data due to not being completely out of the team, where I was wrong! Node T; T.x=i; T.y=j; t.flag=0; Q.push (t); Visit[i][j]=1; while (! Q.empty ()) {T=q.front (); int tx,ty; Tx=t.x;ty=t.y; if (map[tx][ty]== ' R ') {return t.flag; } q.pop (); Node Temp[4]; int p=0,k; for (k=0;k<4;++k) {if (map[tx+u[k]][ty+v[k]]!= ' # ' &&!visit[tx+u[k]][ty+v[k]) {if (map[tx+u[k]][ty+v[k]]= = ' x ') {temp[p].x=tx+u[k]; TEMP[P].Y=TY+V[K]; Visit[temp[p].x][temp[p].y]=1; temp[p].flag=t.flag+2; ++p; } else{Temp[p].x=tx+u[k]; TEMP[P].Y=TY+V[K]; Visit[temp[p].x][temp[p].y]=1; temp[p].flag=t.flag+1; ++p; }//q.push (tEMP); It is not possible to queue directly here, because the same level of R is less time consuming than X, so the four points above and below the T are stored in the array}} sort (temp,temp+p,cmp); Queue for (k=0;k<p;++k) {Q.push (temp[k]) after arranging; }} return-1;} int main () {int n,m;while (~scanf ("%d%d", &n,&m)) {int i,j;for (i=0;i<=n+1;++i) {for (j=0;j<=m+1;++j) {map[ i][j]= ' # ';}} int Ax,ay;for (i=1;i<=n;++i) {GetChar (); for (j=1;j<=m;++j) {scanf ("%c", &map[i][j]), if (map[i][j]== ' a ') ax=i , Ay=j;}} memset (visit,0,sizeof (visit)); int Res=bfs (Ax,ay); if (res==-1) printf ("Poor ANGEL have to stay in the prison all his life.\n" ); else printf ("%d\n", res);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj-1242-rescue "wide Search + priority queue"