hdu1242Rescue
http://acm.hdu.edu.cn/showproblem.php?pid=1242
刚开始由于 没标记已走过的 导致RE TLE 纠结了好久 同样纠结为什么正着搜就WA
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include<string.h> 3 int n, m,p = 0,q = 0; 4 char c[201][201]; 5 typedef struct queue 6 { 7 int num, x, y; 8 }st; 9 st Q[100001]; 10 void inque(int a, int b) 11 { 12 q++; 13 Q[q].x = a; 14 Q[q].y = b; 15 } 16 int main() 17 { 18 int i,j,r[2],k = 0,mov[2]={-1,1},flag =0; 19 while(scanf("%d%d%*c", &n, &m)!=EOF) 20 { 21 flag = 0; 22 memset(Q,0,sizeof(Q[0])); 23 for(i = 1 ; i <= n ; i++) 24 { 25 for(j = 1 ; j <= m ; j++) 26 { 27 scanf("%c", &c[i][j]); 28 if(c[i][j] == 'a') 29 { 30 r[0] = i; 31 r[1] = j; 32 } 33 } 34 getchar(); 35 } 36 p = 0; 37 q = 1; 38 Q[q].x = r[0]; 39 Q[q].y = r[1]; 40 Q[q].num = 0; 41 do 42 { 43 p++; 44 int x1 = Q[p].x; 45 int y1 = Q[p].y; 46 for(j = 0 ; j < 2 ; j++) 47 { 48 if(x1+mov[j]>0&&x1+mov[j]<=n&&(c[x1+mov[j]][y1]=='.'||c[x1+mov[j]][y1]=='x'||c[x1+mov[j]][y1]=='r')) 49 { 50 inque(x1+mov[j],y1); 51 if(c[x1+mov[j]][y1]=='x') 52 Q[q].num = Q[p].num+2; 53 else 54 Q[q].num = Q[p].num+1; 55 if(c[x1+mov[j]][y1]=='r') 56 { 57 flag = 1; 58 break; 59 } 60 c[x1+mov[j]][y1] = '#'; 61 } 62 if(y1+mov[j]>0&&y1+mov[j]<=m&&(c[x1][y1+mov[j]]=='.'||c[x1][y1+mov[j]]=='x'||c[x1][y1+mov[j]]=='r')) 63 { 64 inque(x1,y1+mov[j]); 65 if(c[x1][y1+mov[j]]=='x') 66 Q[q].num = Q[p].num+2; 67 else 68 Q[q].num = Q[p].num+1; 69 if(c[x1][y1+mov[j]]=='r') 70 { 71 flag = 1; 72 break; 73 } 74 c[x1][y1+mov[j]] = '#'; 75 } 76 } 77 if(flag == 1) 78 break; 79 }while(p!=q); 80 if(!flag) 81 printf("Poor ANGEL has to stay in the prison all his life.\n"); 82 else 83 { 84 printf("%d\n",Q[q].num); 85 } 86 } 87 return 0; 88 }