题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1253
广搜题
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <algorithm> 5 #define inf 0x6ffffff 6 #define N 55 7 using namespace std; 8 9 int maps[N][N][N],vis[N][N][N]; 10 11 int dir[6][3]={ {0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0} }; 12 13 int A,B,C,t; 14 15 struct node 16 { 17 int x,y,z,step; 18 }; 19 20 bool judge(int x,int y,int z) 21 { 22 return x<A&&x>=0&&y<B&&y>=0&&z<C&&z>=0&&maps[x][y][z]==0&&vis[x][y][z]==0; 23 } 24 25 int bfs(node s,node e) 26 { 27 queue<node> Q; 28 node q; 29 memset(vis,0,sizeof(vis)); 30 vis[s.x][s.y][s.z]=1; 31 Q.push(s); 32 while(!Q.empty()) 33 { 34 q=Q.front(); 35 Q.pop(); 36 if(q.x==e.x&&q.y==e.y&&q.z==e.z && q.step <=t) 37 return q.step; 38 39 if(q.step > t) 40 return -1; 41 42 for(int i=0;i<6;i++) 43 { 44 s.x=q.x+dir[i][0]; 45 s.y=q.y+dir[i][1]; 46 s.z=q.z+dir[i][2]; 47 if(judge(s.x, s.y, s.z)) 48 { 49 vis[s.x][s.y][s.z]=1; 50 s.step=q.step+1; 51 Q.push(s); 52 } 53 } 54 } 55 return -1; 56 57 } 58 59 int main() 60 { 61 int T,i,j,k,ans; 62 node s,e; 63 scanf("%d",&T); 64 while(T--) 65 { 66 memset(maps,0,sizeof(maps)); 67 68 scanf("%d %d %d %d",&A,&B,&C,&t); 69 for(i=0;i<A;i++) 70 { 71 for(j=0;j<B;j++) 72 { 73 for(k=0;k<C;k++) 74 { 75 scanf("%d",&maps[i][j][k]); 76 } 77 } 78 } 79 s.x=s.y=s.z=s.step=0; 80 e.x=A-1;e.y=B-1;e.z=C-1; 81 82 ans=bfs(s,e); 83 84 if(A+B+C-3>t) 85 ans=-1; 86 printf("%d\n",ans); 87 } 88 return 0; 89 }
时间: 2024-12-31 03:47:45