标签:
1 #include<cstdio> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 int m,n,i,j,ans,map[205][205],ex,ey,bx,by; 6 int dx[4]={-1,1,0,0}; 7 int dy[4]={0,0,-1,1}; 8 char str[205]; 9 struct stu 10 { 11 int x,y,step; 12 bool friend operator <(stu a,stu b) 13 { 14 return a.step>b.step; 15 } 16 }st; 17 int bfs() 18 { 19 priority_queue<stu>que; 20 int x,y,i,time; 21 stu next; 22 23 st.x=bx; 24 st.y=by; 25 st.step=0; 26 que.push(st); 27 while(!que.empty()) 28 { 29 30 st=que.top(); 31 que.pop(); 32 time=st.step; 33 x=st.x; 34 y=st.y; 35 for(i=0;i<4;i++) 36 { 37 st.x=x+dx[i]; 38 st.y=y+dy[i]; 39 if(st.x>=0 &&st.y>=0&&st.x<m&&st.y<m&&map[st.x][st.y]!=0) 40 { 41 if(st.x == ex && st.y == ey) 42 { 43 return time+1; 44 } 45 if(map[st.x][st.y] == 2) 46 { 47 st.step=time+2; 48 } 49 else 50 { 51 st.step=time+1; 52 } 53 map[st.x][st.y]=0; 54 que.push(st); 55 } 56 } 57 } 58 return 0; 59 } 60 int main() 61 { 62 while(scanf("%d %d",&m,&n)!=EOF) 63 { 64 memset(map,0,sizeof(map)); 65 for(i = 0 ; i < m ; i++) 66 { 67 scanf("%s",&str); 68 for(j = 0 ;j < n ; j++) 69 { 70 if(str[j] == ‘.‘) 71 { 72 map[i][j]=1; 73 } 74 if(str[j] == ‘x‘) 75 { 76 map[i][j]=2; 77 } 78 if(str[j] == ‘a‘) 79 { 80 map[i][j]=1; 81 ex=i; 82 ey=j; 83 } 84 if(str[j] == ‘r‘) 85 { 86 map[i][j]=1; 87 bx=i; 88 by=j; 89 } 90 } 91 } 92 /* for( i = 0 ; i < m ; i++) 93 { 94 for(j =0 ; j < n ; j++) 95 { 96 printf("%d ",map[i][j]); 97 if(j == n-1) 98 { 99 printf("\n"); 100 } 101 } 102 } */ 103 map[bx][by]=0; 104 ans=bfs(); 105 if(ans) 106 printf("%d\n",ans); 107 else 108 printf("Poor ANGEL has to stay in the prison all his life.\n"); 109 110 } 111 }
标签:
原文地址:http://www.cnblogs.com/yexiaozi/p/5719387.html