直接贴代码啦!
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #define INF 0x3f3f3f3f #define Pn printf("\n") #define CSH(a, b) memset(a, (b), sizeof(a)) #define LL long long using namespace std; char maze[202][202]; struct node { int x; int y; int num; bool operator < (const node &e) const { return num>e.num; } }a,b; int n,m,ax,ay; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; int bfs() {priority_queue<node> q ; q.push(a); // a.x=ax;a.y=ay;a.num=0; while(!q.empty()) { a=q.top(); q.pop(); // if(maze[a.x][a.y]=='r') //return a.num; for(int i=0;i<4;i++) { b.x=a.x+dx[i]; b.y=a.y+dy[i]; if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&maze[b.x][b.y]!='#') { if(maze[b.x][b.y]=='r') { b.num=a.num+1; return b.num; } else if(maze[b.x][b.y]=='.') { b.num=a.num+1; //q.push(b); } else if(maze[b.x][b.y]=='x') { b.num=a.num+2; // q.push(b); } maze[b.x][b.y]='#'; q.push(b); } } } return -1; } int main() { while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%s",maze[i]); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(maze[i][j]=='a') { a.x=i; a.y=j; a.num=0; maze[i][j]='#'; } } } int t=bfs(); if(t>0) printf("%d\n",t); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; }