标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 int xx[]={1,-1,0,0,0,0}; 7 int yy[]={0,0,1,-1,0,0}; 8 int zz[]={0,0,0,0,1,-1}; 9 int p[55][55][55],vis[55][55][55],a,b,c; 10 struct node 11 { 12 int x; 13 int y; 14 int z; 15 }; 16 int bfs(node s,node f) 17 { 18 memset(vis,0,sizeof(vis)); 19 node cmp,tmp; 20 queue<node>q; 21 q.push(s); 22 while(!q.empty()) 23 { 24 cmp=q.front(); 25 q.pop(); 26 for(int i=0; i<6; i++) 27 { 28 if(vis[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]==0&&cmp.x+xx[i]>=0&&cmp.x+xx[i]<a&&cmp.y+yy[i]>=0&&cmp.y+yy[i]<b&&cmp.z+zz[i]>=0&&cmp.z+zz[i]<c&&p[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]!=1) 29 { 30 vis[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]=vis[cmp.x][cmp.y][cmp.z]+1; 31 tmp.x=cmp.x+xx[i],tmp.y=cmp.y+yy[i],tmp.z=cmp.z+zz[i]; 32 q.push(tmp); 33 } 34 } 35 } 36 if(vis[f.x][f.y][f.z]!=0) 37 { 38 return vis[f.x][f.y][f.z]; 39 } 40 else 41 { 42 return -1; 43 } 44 } 45 int main() 46 { 47 #ifdef CDZSC_OFFLINE 48 freopen("in.txt","r",stdin); 49 #endif 50 int kk,t,i,j,k,sum_time; 51 node s,f; 52 scanf("%d",&kk); 53 while(kk--) 54 { 55 scanf("%d%d%d%d",&a,&b,&c,&t); 56 for(i=0; i<a; i++) 57 { 58 for(j=0; j<b; j++) 59 { 60 for(k=0; k<c; k++) 61 { 62 scanf("%d",&p[i][j][k]); 63 } 64 } 65 } 66 s.x=0,s.y=0,s.z=0; 67 f.x=a-1,f.y=b-1,f.z=c-1; 68 sum_time=bfs(s,f); 69 if(sum_time>t||sum_time==-1) 70 { 71 printf("-1\n"); 72 } 73 else 74 { 75 printf("%d\n",sum_time); 76 } 77 } 78 return 0; 79 } 80 81
标签:
原文地址:http://www.cnblogs.com/Wing0624/p/4253938.html