1#include<stdio.h>
2#include<stdlib.h>
3struct pp
4{
5 int x,y,z,t;
6 struct pp * next;
7};
8int n,m,b,time;
9int map[50][50][50];
10int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
11int bfs(struct pp * head)
12{
13 int d,e,f,i;
14 struct pp *p,*q;
15 p=head->next;
16 while(head->next)
17 {
18 for(i=0;i<6;i++)
19 {
20 d=head->next->x+dir[i][0];
21 e=head->next->y+dir[i][1];
22 f=head->next->z+dir[i][2];
23 if(d>=0&&d<b&&e>=0&&e<n&&f>=0&&f<m&&map[d][e][f]==0)
24 {
25 map[d][e][f]=1;
26 if(d==b-1&&e==n-1&&f==m-1)
27 return head->next->t+1;
28 q=(struct pp *)malloc(20);
29 q->t=head->next->t+1;
30 if(q->t>=time)
31 continue;
32 q->x=d;
33 q->y=e;
34 q->z=f;
35 q->next=NULL;
36 p->next=q;
37 p=q;
38 }
39 }
40 head->next=head->next->next;
41 }
42 return 0;
43}
44int main()
45{
46 int i,j,l,t,num;
47 struct pp head;
48 struct pp *p;
49 scanf("%d",&t);
50 while(t--)
51 {
52 scanf("%d%d%d%d",&b,&n,&m,&time);
53 for(i=0;i<b;i++)
54 for(j=0;j<n;j++)
55 for(l=0;l<m;l++)
56 scanf("%d",&map[i][j][l]);
57 if(b+m+n-3>time)
58 {puts("-1");continue;}
59 if(n==1&&m==1&&b==1)
60 {puts("0");continue;}
61 p=(struct pp *)malloc(20);
62 head.next=p;;
63 p->next=NULL;
64 p->x=0;p->y=0;p->z=0;p->t=0;
65 num=bfs(&head);
66 free(p);
67 if(num)
68 printf("%d\n",num);
69 else
70 printf("-1\n");
71 }
72}
posted on 2009-01-18 22:24
混沌的云 阅读(270)
评论(0) 编辑 收藏 引用