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.)
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 Input7 8#.#####.#.a#. r.#. #x ..... #.##...##...#..............
Sample Output13 bfs+ Priority Queue ... the reason that BFS can find the best solution is ... Shorter time points must first access to the first queue ... But because of the presence of guards ... The node that is first accessed is not a shorter node if it is guarded ... So this time we're going to use the priority queue ... Write for the first time. Then I learned the writing of heavy-duty < ... Written in the structure and outside the body. Feel Q teacher ... The nest is just to ask him this overload in the writing structure of how to ... He just sent me a copy of Kuangbin's template and learned a lot. There is, because there are many friends, and little angel only one ... Let the little Angels find Friends ... Reverse thinking ... Once again AC, happy.
1 /*************************************************************************2 > File name:code/hdu/1242.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 October 02 Friday 14:38 17 Seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + using namespacestd; A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - #defineLR DYING111QQZ - Const intdx4[4]={1,0,0,-1}; - Const intdy4[4]={0,-1,1,0}; -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to Const intn=2e2+5; + CharMaze[n][n]; - intM,n; the intans; * BOOLVis[n][n]; $ structnodePanax Notoginseng { - intx, y; the intD; + A BOOLOK () the { + if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&maze[x][y]!='#')return true; - return false; $ } $ BOOLHasguard () - { - if(maze[x][y]=='x')return true; the return false; - }Wuyi BOOLgoal () the { - if(maze[x][y]=='R')return true; Wu return false; - } About }s; $ - BOOL operator< (ConstNode &a,ConstNode &b)//Heavy-duty priority < relationships ... Returns True when the priority is smaller and later in the queue. - { - returnA.d>B.D; A } + the BOOLBFS () - { $Priority_queue<node>Q; the Q.push (s); the the while(!q.empty ()) the { -Node Pre =q.top (); in Q.pop (); the //cout<<pre.x<< "" <<pre.y<< "" <<pre.d<<endl; the if(Pre.goal ()) About { theAns =PRE.D; the return true; the } + - for(inti =0; I <4; i++) the {Bayi node Next; theNext.x = pre.x +Dx4[i]; theNext.y = Pre.y +Dy4[i]; -NEXT.D = PRE.D +1; - if(!next.ok ())Continue; theVIS[NEXT.X][NEXT.Y] =true; the if(Next.hasguard ()) next.d++; the Q.push (next); the - the } the } the return false;94 } the intMain () the { the #ifndef Online_judge98Freopen ("In.txt","R", stdin); About #endif - while(SCANF ("%d%d", &n,&m)! =EOF) {101MS (VIS,false);102 for(inti =0; I < n; i++) scanf ("%s", Maze[i]);103 for(inti =0; I < n; i++)104 { the for(intj =0; J < M; J + +)106 {107 if(maze[i][j]=='a')108 {109S.x =i; theS.y =J;111S.D =0 ; theVIS[I][J] =true;113 Break; the } the } the }117 118 if(BFS ())119 { -printf"%d\n", ans);121 }122 Else123 {124Puts"Poor ANGEL have to stay in the prison all he life."); the }126 }127 #ifndef Online_judge - fclose (stdin);129 #endif the return 0;131}
View Code
HDU 1242 Rescue (bfs+ priority queue)