标签:
1 #include<stdio.h> 2 #include<queue> 3 using namespace std; 4 struct Node{ 5 int nx,ny,step; 6 friend bool operator < (Node a,Node b){ 7 return a.step>b.step; 8 } 9 }; 10 Node a,b; 11 char map[250][250]; 12 int disx[5]={0,1,-1,0}; 13 int disy[5]={1,0,0,-1}; 14 int N,M,sx,sy,ex,ey,minstep,flot; 15 void bfs(){ 16 priority_queue<Node>dl; 17 a.nx=sx;a.ny=sy;a.step=0; 18 dl.push(a); 19 while(!dl.empty()){ 20 a=dl.top(); 21 dl.pop(); 22 map[a.nx][a.ny]=‘#‘; 23 if(a.nx==ex&&a.ny==ey){flot=1; 24 minstep=a.step; 25 return; 26 } 27 for(int i=0;i<4;i++){ 28 b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.step=a.step+1; 29 if(b.nx<0||b.ny<0||b.nx>=N||b.ny>=M||map[b.nx][b.ny]==‘#‘)continue; 30 if(map[b.nx][b.ny]==‘.‘||map[b.nx][b.ny]==‘r‘)dl.push(b); 31 else{ 32 b.step++;dl.push(b); 33 } 34 } 35 } 36 } 37 int main(){ 38 while(~scanf("%d%d",&N,&M)){minstep=0;flot=0; 39 for(int i=0;i<N;i++)scanf("%s",map[i]); 40 for(int x=0;x<N;x++){ 41 for(int y=0;y<M;y++){ 42 if(map[x][y]==‘a‘)sx=x,sy=y; 43 if(map[x][y]==‘r‘)ex=x,ey=y; 44 } 45 } 46 bfs(); 47 if(flot)printf("%d\n",minstep); 48 else puts("Poor ANGEL has to stay in the prison all his life."); 49 } 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4706225.html