Input输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)
特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
Output对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
Sample Input
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
Sample Output
11
并没有起点为1或门为1的情况。
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 int f[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; 6 queue<int>s; 7 int b[51][51][51]; 8 int x,y,z,t,q,Z,X,Y,time; 9 bool vis[51][51][51]; 10 int bfs() 11 { 12 13 Z=0; 14 X=0; 15 Y=0; 16 time=0; 17 s.push(Z); 18 s.push(X); 19 s.push(Y); 20 s.push(time); 21 while(!s.empty()) 22 { 23 Z=s.front(); 24 s.pop(); 25 X=s.front(); 26 s.pop(); 27 Y=s.front(); 28 s.pop(); 29 time=s.front(); 30 s.pop(); 31 if(time>t) 32 return -1; 33 if(Z==z-1&&X==x-1&&Y==y-1&&time<=t) 34 return time; 35 for(int l=0;l<6;l++) 36 { 37 int X1=X+f[l][1]; 38 int Z1=Z+f[l][0]; 39 int Y1=Y+f[l][2]; 40 if(Z1>=0&&Z1<z&&X1>=0&&X1<x&&Y1>=0&&Y1<y) 41 { 42 if(!b[Z1][X1][Y1]&&!vis[Z1][X1][Y1]) 43 { 44 s.push(Z1); 45 s.push(X1); 46 s.push(Y1); 47 s.push(time+1); 48 vis[Z1][X1][Y1]=true; 49 } 50 } 51 52 } 53 } 54 return -1; 55 } 56 int main() 57 { 58 int i,j,k,T; 59 scanf("%d",&T); 60 while(T--) 61 { 62 scanf("%d%d%d%d",&z,&x,&y,&t); 63 for(i=0;i<z;i++) 64 { 65 for(j=0;j<x;j++) 66 { 67 for(k=0;k<y;k++) 68 { 69 scanf("%d",&b[i][j][k]); 70 } 71 } 72 } 73 b[0][0][0]=true; 74 memset(vis,false,sizeof(vis)); 75 q=bfs(); 76 while(s.size()) 77 s.pop(); 78 printf("%d\n",q); 79 } 80 return 0; 81 }