Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16718 Accepted Submission(s):
6069
1 /* 2 Name: Rescue 3 Copyright: Shangli_Cloud 4 Author: Shangli_Cloud 5 Date: 24/10/14 22:44 6 Description: 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cstring> 11 #include<algorithm> 12 #include<cmath> 13 #include<set> 14 //#include<map> 15 #include<queue> 16 #include<stack> 17 #include<vector> 18 using namespace std; 19 const int ms=201; 20 const int inf=0x7fffffff; 21 int dir[4][2]={0,1,1,0,0,-1,-1,0}; 22 int min_time[ms][ms]; 23 char map[ms][ms]; 24 struct point 25 { 26 int x,y,time,step; 27 }; 28 queue<point> que; 29 int n,m; 30 int ax,ay,sx,sy; 31 void input() 32 { 33 //scanf("%d%d",&n,&m); 34 getchar(); 35 for(int i=0;i<n;i++) 36 { 37 for(int j=0;j<m;j++) 38 { 39 min_time[i][j]=inf; 40 scanf("%c",&map[i][j]); 41 if(map[i][j]==‘a‘) 42 { 43 ax=i; 44 ay=j; 45 } 46 else if(map[i][j]==‘r‘) 47 { 48 sx=i; 49 sy=j; 50 } 51 } 52 getchar(); 53 } 54 min_time[sx][sy]=0; 55 return ; 56 } 57 int dfs(point p) 58 { 59 point pp; 60 que.push(p); 61 while(!que.empty()) 62 { 63 pp=que.front(); 64 que.pop(); 65 for(int i=0;i<4;i++) 66 { 67 int x=pp.x+dir[i][0]; 68 int y=pp.y+dir[i][1]; 69 if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!=‘#‘) 70 { 71 point t; 72 t.x=x; 73 t.y=y; 74 t.step=pp.step+1; 75 t.time=pp.time+1; 76 if(map[x][y]==‘x‘) 77 t.time++; 78 if(t.time<min_time[x][y]) 79 { 80 min_time[x][y]=t.time; 81 que.push(t); 82 } 83 } 84 } 85 } 86 return min_time[ax][ay]; 87 } 88 int main() 89 { 90 int i,j; 91 while(scanf("%d%d",&n,&m)!=EOF) 92 { 93 input(); 94 point start; 95 start.x=sx; 96 start.y=sy; 97 start.time=0; 98 start.step=0; 99 int ans=dfs(start); 100 if(ans<inf) 101 printf("%d\n",ans); 102 else 103 printf("Poor ANGEL has to stay in the prison all his life.\n"); 104 } 105 return 0; 106 }
原文:http://www.cnblogs.com/767355675hutaishi/p/4049513.html