#include <iostream>#include<string>#include<string.h>#include<stdio.h>#include<math.h>#include<queue>#include<algorithm>using namespacestd;intN,m,di,dj,ok,ss;Charmapp[ About][ About];intvis[ About][ About];//There should be more than one friendstructnode{intx, y; intstep; FriendBOOL operator<(node A,node b) {returnA.step > B.step;//Ascending }};intdir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};p riority_queue<node>PQ;voidBFS () {node temp,next; ints,d; while(!Pq.empty ()) {Temp=Pq.top (); //cout<<temp.floor<< "<<temp.x<<" "<<temp.y<<" "<<temp.step<< Endl;Pq.pop (); if(temp.x==di&&temp.y==DJ) { } for(intI=0;i<4; i++) {s=temp.x+dir[i][0]; D=temp.y+dir[i][1]; if(s==di&&d==DJ) {OK=1; SS=temp.step+1; Break;} if(s>=0&&s<n&&d>=0&&d<m&&vis[s][d]==0&&mapp[s][d]!='#'){ if(mapp[s][d]=='.') {Next.step=temp.step+1; }Else if(mapp[s][d]=='x') {Next.step=temp.step+2; } next.x=s; Next.y=D; Pq.push (next); VIS[S][D]=1; } } if(OK) Break; }}intMain () {intSI,SJ; while(cin>>n>>m) {memset (Vis,0,sizeof(VIS)); while(!pq.empty ()) Pq.pop (); OK=0; SS=0; for(intI=0; i<n;i++) for(intj=0; j<m;j++) {cin>>Mapp[i][j]; if(mapp[i][j]=='a') {di=i;dj=J;} if(mapp[i][j]=='R') {si=i;sj=J;} } node TT; Tt.x=si;tt.y=sj;tt.step=0; VIS[SI][SJ]=1; Pq.push (TT); BFS (); if(ss) cout<<ss<<Endl; Elsecout<<"Poor ANGEL have to stay in the prison all he life."<<Endl; } return 0;}
1242 Rescue BFS