标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20821 Accepted Submission(s): 7429
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 8 int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1}; 9 char map[220][220]; int vis[220][220]; 10 int i, j, m, n; 11 12 struct prision 13 { 14 int x, y, time; 15 friend bool operator < (prision x, prision y) 16 { 17 return x.time > y.time; //从小 → 大; 18 } 19 } current, next; 20 21 22 int BFS(int x,int y) 23 { 24 25 26 priority_queue <prision>q; 27 prision current,next; 28 memset(vis,0,sizeof(vis)); 29 30 current.x=x; 31 current.y=y; 32 current.time=0; 33 vis[current.x][current.y]=1; 34 q.push(current); 35 36 37 while(!q.empty()) 38 { 39 40 current=q.top(); 41 q.pop(); 42 for(int i=0;i<4;i++) 43 { 44 next.x=current.x+dir[i][0]; 45 next.y=current.y+dir[i][1]; 46 47 if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!=‘#‘&&!vis[next.x][next.y]) 48 { 49 50 if(map[next.x][next.y]==‘r‘) 51 return current.time+1; 52 53 if(map[next.x][next.y]==‘x‘) 54 next.time=current.time+2; 55 else 56 next.time=current.time+1; 57 vis[next.x][next.y]=1; 58 q.push(next); 59 } 60 } 61 } 62 return -1; 63 } 64 65 66 int main() 67 { 68 prision angle; 69 while(~scanf("%d %d", &n, &m)) 70 { 71 for(i=0; i<n; i++) 72 { 73 for(j=0; j<m; j++) 74 { 75 cin >> map[i][j]; 76 if(map[i][j] == ‘a‘) 77 { 78 angle.x = i; 79 angle.y = j; 80 } 81 } 82 } 83 int time = BFS(angle.x, angle.y); 84 85 if(time == -1) 86 cout <<"Poor ANGEL has to stay in the prison all his life."<<endl; 87 else 88 cout <<time<< endl; 89 } 90 return 0; 91 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4690331.html