1 #include <iostream>
2 #include <queue>
3 #include <stdio.h>
4 #include <string.h>
5 using namespace std;
6
7 struct node
8 {
9 int x,y,z;
10 int time;
11 };
12
13 int a,b,c,t;
14 int map[60][60][60];
15 //int visited[60][60][60];
16 int walk[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
17
18 int bfs()
19 {
20 queue<node>Q;
21 node u;
22 u.x=u.y=u.z=u.time=0;
23 Q.push(u);
24 while(!Q.empty())
25 {
26 u=Q.front();
27 Q.pop();
28 if(u.time==t) continue;
29 for(int i=0;i<6;++i)
30 {
31 int xx=u.x+walk[i][0];
32 int yy=u.y+walk[i][1];
33 int zz=u.z+walk[i][2];
34 if(xx>-1&&xx<a&&yy>-1&&yy<b&&zz>-1&&zz<c &&(!map[xx][yy][zz]))
35 {
36 if(xx==a-1 && yy==b-1 && zz==c-1) return u.time+1;
37 else
38 {
39 node v;
40 v.x=xx;
41 v.y=yy;
42 v.z=zz;
43 v.time=u.time+1;
44 Q.push(v);
45 map[xx][yy][zz]=1;//走过之后把格子设为1 之后就不能再走 剩下visited数组
46 }
47 }
48 }
49 }
50
51 return -1;
52 }
53
54 int main()
55 {
56 int k,ans;
57 cin>>k;
58 while(k--)
59 {
60 int ans=0;
61 scanf("%d %d %d %d",&a,&b,&c,&t);
62 for(int i=0;i<a;++i)
63 {
64 for(int j=0;j<b;++j)
65 {
66 for(int p=0;p<c;++p)
67 {
68 scanf("%d",&map[i][j][p]);
69 ans+=(!map[i][j][p]);
70 }
71 }
72 }
73 if((a+b+c-3)>t || map[a-1][b-1][c-1] || ans<(a+b+c-3))//剪枝
74 {
75 printf("-1\n");
76 }
77 else
78 {
79 ans=bfs();
80 printf("%d\n",ans);;
81 }
82 }
83 }