标签:
Description
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 int n,m,ans; 8 char a[222][222]; 9 bool b[222][222]; 10 int nx[4]={-1,1,0,0}; 11 int ny[4]={0,0,-1,1}; 12 struct node{ 13 int x; 14 int y; 15 int step; 16 }; 17 int BFS(int x,int y) 18 { 19 node s; 20 s.x=x; 21 s.y=y; 22 s.step=0; 23 b[x][y]=true; 24 queue<node>q; 25 q.push(s); 26 while(!q.empty()) 27 { 28 node now=q.front(); 29 q.pop(); 30 if(a[now.x][now.y]==‘r‘) 31 return now.step; 32 for(int i = 0;i < 4;i++) 33 { 34 node end; 35 end.x=now.x+nx[i]; 36 end.y=now.y+ny[i]; 37 end.step=now.step; 38 if(b[end.x][end.y]==false&&(a[end.x][end.y]==‘.‘||a[end.x][end.y]==‘r‘)) 39 { 40 b[end.x][end.y]=true; 41 end.step+=1; 42 q.push(end); 43 } 44 else if(b[end.x][end.y]==false&&a[end.x][end.y]==‘x‘) 45 { 46 b[end.x][end.y]=true; 47 end.step+=2; 48 q.push(end); 49 } 50 } 51 } 52 return -1; 53 54 } 55 int main() 56 { 57 while(scanf("%d %d",&n,&m)!=EOF) 58 { 59 int i,j; 60 memset(b,false,sizeof(b)); 61 for(i = 0;i < n;i++) 62 scanf("%s",a[i]); 63 for(i = 0;i < n;i++) 64 for(j = 0;j < m;j++) 65 { 66 if(a[i][j]==‘a‘)/*让a找r,因为r可能不止一个,而a只有一个*/ 67 { 68 ans=BFS(i,j); 69 break; 70 } 71 } 72 if(ans == -1) 73 printf("Poor ANGEL has to stay in the prison all his life.\n"); 74 else 75 printf("%d\n",ans); 76 77 78 } 79 return 0; 80 }
标签:
原文地址:http://www.cnblogs.com/llal/p/5719562.html