hdu 2102 BFS
思路:bfs搜一发
AC代码:
1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x > 0 ? x : -x 9 #define max(a,b) a > b ? a : b 10 #define min(a,b) a < b ? a : b 11 12 using namespace std; 13 14 int di[6][3] = {{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}}; 15 int Map[5][15][15]; 16 bool vis[5][15][15]; 17 int t; 18 19 struct Node 20 { 21 int zz,xx,yy; 22 int step; 23 }; 24 25 void Bfs() 26 { 27 memset(vis,0,sizeof(vis)); 28 queue<Node>Q; 29 Node now,next; 30 int l,r; 31 32 now.zz=1; 33 now.xx=1; 34 now.yy=1; 35 now.step=0; 36 vis[1][1][1] = 1; 37 38 Q.push(now); 39 40 while(!Q.empty()) 41 { 42 now = Q.front(); 43 Q.pop(); 44 45 if(Map[now.zz][now.xx][now.yy]==3) 46 { 47 if(now.step<=t) 48 printf("YES\n"); 49 else 50 printf("NO\n"); 51 return; 52 } 53 if(Map[now.zz][now.xx][now.yy]==2) 54 l=4,r=6; 55 if(Map[now.zz][now.xx][now.yy]==1) 56 l=0,r=4; 57 58 for(int i=l; i<r; i++) 59 { 60 next.zz = now.zz + di[i][0]; 61 next.xx = now.xx + di[i][1]; 62 next.yy = now.yy + di[i][2]; 63 next.step = now.step + 1; 64 if(i==4||i==5) 65 next.step = now.step; 66 67 if(Map[next.zz][next.xx][next.yy]!=0) 68 { 69 if(!vis[next.zz][next.xx][next.yy]) 70 { 71 vis[next.zz][next.xx][next.yy] = 1; 72 Q.push(next); 73 } 74 } 75 } 76 } 77 printf("NO\n"); 78 } 79 80 int main() 81 { 82 int c,n,m,i,j,k; 83 char s; 84 scanf("%d",&c); 85 while(c--) 86 { 87 memset(Map,0,sizeof(Map)); 88 scanf("%d%d%d",&n,&m,&t); 89 for(i=1; i<=2; i++) 90 { 91 for(j=1; j<=n; j++) 92 { 93 getchar(); 94 for(k=1; k<=m; k++) 95 { 96 scanf("%c",&s); 97 if(s=='S'||s=='.') 98 Map[i][j][k] = 1; 99 if(s=='P') 100 Map[i][j][k] = 3; 101 if(s=='#') 102 Map[i][j][k] = 2; 103 if(s=='*') 104 Map[i][j][k] = 0; 105 } 106 } 107 getchar(); 108 } 109 110 Bfs(); 111 } 112 return 0; 113 }