Topic Link: Click Open link http://acm.hdu.edu.cn/showproblem.php?pid=1242
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < Vector> #include <queue>using namespace std;const int n=220;int N,m,stx,sty,endx,endy;char mp[n][n];int Vis[N] [N]; int f[4][2]={{0,-1},{-1,0},{0,1},{1,0}};//kengstruct node{int x;int y;int t; friend bool operator < (const node &a, Const node &b) {return a.t>b.t;//is greater than from small to large rows}}; bool Isbound (int x,int y) {if (x>0&&x<=n&&y >0&&y<=m&&mp[x][y]!= ' # ') return True;else return false;} void BFS (int x,int y) {priority_queue<node>q;memset (vis,0,sizeof (VIS)); Q.empty ()) {Q.pop ();} Node p,q;p.x=x;p.y=y;p.t=0;vis[p.x][p.y]=1; Q.push (P); while (! Q.empty ()) {p=q.top (); Q.pop (); if (P.x==endx&&p.y==endy) {printf ("%d\n", p.t); return;} for (int i=0;i<4;i++) {q.x=p.x+f[i][0];q.y=p.y+f[i][1];if (!isbound (Q.X,Q.Y)) continue;if (vis[q.x][q.y]==1) continue;vis[q.x][q.y]=1;if (mp[q.x][q.y]== ' x ') q.t =p.t + 2;elseq.t =p.t + 1; Q.puSH (q);}} printf ("Poor ANGEL have to stay in the prison all his life.\n");} int main () {while (~scanf ("%d%d", &n,&m)) {for (int. i=1;i<=n;i++) {for (int j=1;j<=m;j++) {Cin>>mp[i] [J];if (mp[i][j]== ' R ') {stx=i;sty=j;} else if (mp[i][j]== ' a ') {endx=i;endy=j;}}} BFS (stx,sty);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1242 Rescue bfs+ Priority queue