1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 typedef struct dian 6 { 7 friend bool operator<(dian n1,dian n2) 8 { 9 return(n1.time>n2.time); 10 } 11 int x,y,time; 12 }; 13 char map[205][205]; 14 int used[205][205],m,n; 15 int xx[]={1,-1,0,0}; 16 int yy[]={0,0,1,-1}; 17 int bfs(int x1,int y1) 18 { 19 priority_queue<dian>q; dian n1,n2; 20 int i,tx,ty; 21 while (!q.empty()) q.pop(); 22 n1.x=x1; n1.y=y1; n1.time=0; 23 used[x1][y1]=1; q.push(n1); 24 while (!q.empty()) 25 { 26 n1=q.top(); q.pop(); 27 if (map[n1.x][n1.y]=='r') return(n1.time); 28 for (i=0;i<4;i++) 29 { 30 tx=n1.x+xx[i]; ty=n1.y+yy[i]; 31 if (tx>=m||ty>=n||tx<0||ty<0||used[tx][ty]==1 32 ||map[tx][ty]=='#') continue; 33 n2.x=tx; n2.y=ty; n2.time=n1.time+1; 34 if (map[tx][ty]=='x') n2.time++; 35 used[tx][ty]=1; 36 q.push(n2); 37 } 38 } 39 return(-1); 40 } 41 int main() 42 { 43 int i,j,x1,y1,p; 44 while (~scanf("%d%d",&m,&n)) 45 { 46 getchar(); 47 memset(used,0,sizeof(used)); 48 for (i=0;i<m;i++) 49 { 50 gets(map[i]); 51 for (j=0;j<n;j++) 52 if (map[i][j]=='a') {x1=i; y1=j;} 53 } 54 //printf("%d %d",x1,y1); 55 p=bfs(x1,y1); 56 if (p!=-1) printf("%d\n",p); 57 else printf("Poor ANGEL has to stay in the prison all his life.\n"); 58 } 59 }
转载于:https://www.cnblogs.com/xiao-xin/articles/3848978.html