标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22286 Accepted Submission(s): 7919
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 using namespace std; 5 #define MAX 1000000 6 int Map[250][250]; 7 int T[250][250]; 8 int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; 9 int n,m; 10 int si,sj,di,dj; 11 int sign=0; 12 typedef struct pointer 13 { 14 int x,y; 15 int time; 16 struct pointer *next; 17 } LNode,*LinkList; 18 LinkList ptr; 19 void bfs(LinkList head); 20 int main() 21 { 22 int i,j; 23 while(scanf("%d%d",&n,&m)!=EOF) 24 { 25 getchar(); 26 for(i=0; i<n; i++) 27 { 28 for(j=0; j<m; j++) 29 { 30 scanf("%c",&Map[i][j]); 31 T[i][j]=MAX; 32 if(Map[i][j]==‘a‘) 33 { 34 di=i; 35 dj=j; 36 } 37 else if(Map[i][j]==‘r‘) 38 { 39 si=i; 40 sj=j; 41 T[si][sj]=0; 42 } 43 } 44 getchar(); 45 } 46 LinkList p; 47 p=(LinkList)malloc(sizeof(LNode)); 48 p->x=si; 49 p->y=sj; 50 p->time=0; 51 p->next=NULL; 52 sign=0; 53 bfs(p); 54 if(T[di][dj]<MAX)cout<<T[di][dj]<<endl; 55 else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 56 } 57 return 0; 58 } 59 void bfs(LinkList head) 60 { 61 int i,fx,fy; 62 while(head!=NULL) 63 { 64 for(i=0; i<4; i++) 65 { 66 fx=head->x+dir[i][0]; 67 fy=head->y+dir[i][1]; 68 if(fx>=0&&fx<n&&fy>=0&&fy<m&&Map[fx][fy]!=‘#‘) 69 { 70 LinkList p; 71 if(sign==0) ptr=head; 72 p=(LinkList)malloc(sizeof(LNode)); 73 p->x=fx; 74 p->y=fy; 75 p->time=head->time+1; 76 if(Map[fx][fy]==‘x‘) p->time++; 77 if(p->time<T[fx][fy]) 78 { 79 T[fx][fy]=p->time; 80 ptr->next=p; 81 p->next=NULL; 82 ptr=ptr->next; 83 sign=1; 84 } 85 } 86 } 87 head=head->next; 88 } 89 }
标签:
原文地址:http://www.cnblogs.com/GeekZRF/p/4926187.html