~~~~
I suddenly found that all the questions for a search were written. Yesterday, we found a BFS problem. HDU is connected to AC and zoj is connected to WA. I have to say that the data on HDU is water ..
I thought about getting up early today and wrote it in both the queue and the monotonous queue. It's 0ms ~~
~~~~
Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 649
~~~~
The first pitfall is friends. Yes, there are many friends, and there is only one angel. Therefore, a better solution is to start with Angel.
The second is that it takes 2 mins to kill the guard and go to its location. This violates the principle of horizontal expansion of BFS (it should be said that, please sell the guard ).
Therefore, if you use a queue, You can take two steps to kill guard and go to its location, and join the queue twice.
Priority queue is simple, and priority can be set up from small to large.
~~~~
Queue:
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <cmath> # include <string> # include <queue> # define n 222 using namespace STD; int n, m; char G [N] [N]; int dir [4] [2] = {,-, 0,-, 1 }; struct node {int X, Y; int t ;}; int BFS (int x, int y) {queue <node> q; node cur, next; cur. X = x, cur. y = Y, cur. T = 0; q. push (cur); G [x] [Y] = '#'; // indicates whether the VIS array record has been accessed. While (! Q. empty () {cur = Q. front (); q. pop (); If (G [cur. x] [cur. y] = 'y') {G [cur. x] [cur. y] = '#';//~~ Cur. T = cur. t + 1; q. push (cur); // join again to continue;} For (INT I = 0; I <4; I ++) {next. X = cur. X + dir [I] [0], next. y = cur. Y + dir [I] [1]; If (next. x> = 0 & next. x <n & next. y> = 0 & next. Y <M & G [next. x] [next. y]! = '#') {If (G [next. x] [next. y] = '. ') {G [next. x] [next. y] = '#'; next. T = cur. t + 1; q. push (next);} else if (G [next. x] [next. y] = 'X ')//~~ {G [next. x] [next. Y] = 'y ';//~~ Next. T = cur. t + 1; q. push (next);} else if (G [next. x] [next. y] = 'R') return cur. t + 1 ;}}} return-1 ;}int main () {While (~ Scanf ("% d", & N, & M) {int Sx, Sy; For (INT I = 0; I <n; I ++) {scanf ("% s", G [I]); For (Int J = 0; j <m; j ++) if (G [I] [J] = 'A') SX = I, Sy = J;} int T = BFS (sx, Sy); If (T <0) puts ("Poor Angel has to stay in the prison all his life. "); else printf (" % d \ n ", T);} return 0 ;}
Monotonous queue:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<string>#include<queue>#define N 222using namespace std;int n,m;char g[N][N];int dir[4][2]={1,0,-1,0,0,-1,0,1};struct node{ int x,y; int t;};bool operator < (node a,node b) { return a.t>b.t; //~~}int bfs(int x,int y){ priority_queue<node> q; node cur,next; cur.x=x,cur.y=y,cur.t=0; q.push(cur); g[x][y]='#'; while(!q.empty()) { cur=q.top(); q.pop(); for(int i=0;i<4;i++) { next.x=cur.x+dir[i][0],next.y=cur.y+dir[i][1]; if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && g[next.x][next.y]!='#') { if(g[next.x][next.y]=='.') { g[next.x][next.y]='#'; next.t=cur.t+1; q.push(next); } else if(g[next.x][next.y]=='x') { g[next.x][next.y]='#'; next.t=cur.t+2; q.push(next); } else if(g[next.x][next.y]=='r') return cur.t+1; } } } return -1;}int main(){ while(~scanf("%d%d",&n,&m)) { int sx,sy; for(int i=0;i<n;i++) { scanf("%s",g[i]); for(int j=0;j<m;j++) if(g[i][j]=='a') sx=i,sy=j; } int t=bfs(sx,sy); if(t<0) puts("Poor ANGEL has to stay in the prison all his life."); else printf("%d\n",t); } return 0;}