标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22034 Accepted Submission(s): 7843
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 struct Point 8 { 9 int x,y,t; 10 bool operator < (const Point &a)const 11 { 12 return t>a.t; 13 } 14 }; 15 16 char map[205][205]; 17 Point start; 18 int n,m; 19 int diect[][2]={{1,0},{-1,0},{0,1},{0,-1}}; 20 21 int bfs() 22 { 23 priority_queue<Point> que; 24 Point cur,next; 25 int i,j; 26 27 map[start.x][start.y]=‘#‘; 28 que.push(start); 29 while(!que.empty()) 30 { 31 cur=que.top(); 32 que.pop(); 33 for(i=0;i<4;i++) 34 { 35 next.x=cur.x+diect[i][0]; 36 next.y=cur.y+diect[i][1]; 37 next.t=cur.t+1; 38 if(next.x<0 || next.x>=n || next.y<0 || next.y>=m) 39 continue; 40 if(map[next.x][next.y]==‘#‘) 41 continue; 42 if(map[next.x][next.y]==‘r‘) 43 return next.t; 44 if(map[next.x][next.y]==‘.‘) 45 { 46 map[next.x][next.y]=‘#‘; 47 que.push(next); 48 } 49 else if(map[next.x][next.y]==‘x‘) 50 { 51 map[next.x][next.y]=‘#‘; 52 next.t++; 53 que.push(next); 54 } 55 } 56 } 57 return -1; 58 } 59 60 int main() 61 { 62 int i,j,ans; 63 char *p; 64 while(scanf("%d %d",&n,&m)!=EOF) 65 { 66 for(i=0;i<n;i++) 67 { 68 scanf("%s",map[i]); 69 if(p=strchr(map[i],‘a‘)) 70 { 71 start.x=i; 72 start.y=p-map[i]; 73 start.t=0; 74 } 75 } 76 ans=bfs(); 77 if(ans==-1) 78 printf("Poor ANGEL has to stay in the prison all his life.\n"); 79 else 80 printf("%d\n",ans); 81 } 82 return 0; 83 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4817740.html