Zoj1649-rescue (Maze Shortest path) "BFs Priority queue"

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

Rescue Time limit: 2 Seconds Memory Limit: 65536 KB

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

Test instructions: Starting from ' R ', the destination is ' a ', ' # ' cannot be over, '. ' For one step, the special is ' X ' counted as two steps. To find the minimum number of steps.

Idea: BFS. The need to note here is X, because the two-step, in the ordinary BFS queue is not necessarily first out of the shortest step, so you have to use priority queue.

Code:

1#include <fstream>2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cmath>7#include <cstdlib>8#include <queue>9 Ten using namespacestd; One  A #definePI ACOs (-1.0) - #defineEPS 1e-10 - #defineLLL __int64 the #definell Long Long - #defineINF 0x7fffffff -  - structnode{ +     intX,y,step; -     BOOL operator< (ConstNode &r)Const{ +         returnStep>R.step; A     } at }tmp; -  -Priority_queue<node>Qu; - intn,m,x1,y1_; - Charmatrix[205][205]; - BOOLb[205][205]; in Charxy[2][4]={{0,0,1,-1},{1,-1,0,0}}; -  toInlineBOOLCheck (intXinty); + intBfs (); -  the intMain () { *     //freopen ("d:\\input.in", "R", stdin); $     //freopen ("D:\\output.out", "w", stdout);Panax Notoginseng      while(~SCANF ("%d%d",&n,&m)) { -          for(intI=0; i<=m+1; i++) matrix[0][i]='#', matrix[n+1][i]='#'; the          for(intI=0; i<=n+1; i++) matrix[i][0]='#', matrix[i][m+1]='#'; +          for(intI=1; i<=n;i++){ A GetChar (); the              for(intj=1; j<=m;j++){ +matrix[i][j]=GetChar (); -                 if(matrix[i][j]=='R') x1=i,y1_=J; $             } $         } -         intans=Bfs (); -         if(ans==-1) puts ("Poor ANGEL have to stay in the prison all he life."); the         Elseprintf"%d\n", ans); -     }Wuyi     return 0; the } -InlineBOOLCheck (intXinty) { Wu     returnmatrix[x][y]!='#'&&b[x][y]==0; - } About intBfs () { $memset (b,0,sizeof(b)); -      while(!qu.empty ()) Qu.pop (); -tmp.step=0; -tmp.x=X1; Atmp.y=y1_; + Qu.push (TMP); theb[x1][y1_]=1; -      while(!Qu.empty ()) { $tmp=qu.top (); the Qu.pop (); the         intx=tmp.x,y=tmp.y; the         inttx,ty,ts=tmp.step+1; the          for(intI=0;i<4; i++){ -tx=x+xy[0][i]; inty=y+xy[1][i]; the             if(Check (tx,ty)) { the                 if(matrix[tx][ty]=='.'){ Aboutb[tx][ty]=1; thetmp.x=TX; thetmp.y=Ty; thetmp.step=ts; + Qu.push (TMP); -}Else if(matrix[tx][ty]=='x'){ theb[tx][ty]=1;Bayitmp.x=TX; thetmp.y=Ty; thetmp.step=ts+1; - Qu.push (TMP); -}Else{ the                     returnts; the                 } the             } the         } -     } the     return-1; the}
View Code

Zoj1649-rescue (Maze Shortest path) "BFs Priority queue"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.