11
Three-dimensional maze
Search by Breadth First
The code is as follows
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <queue>5#include <iostream>6 using namespacestd;7 8 intdir[][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};9 intcubic[ the][ the][ the];Ten inttcnt[ the][ the][ the]; One A structpoint{ - intx, y, Z, t; -Point (intXintYintZintt) { the This->x = x, This->y = y, This->z = Z, This->t =T; - } - }; - +Queue <Point>que; - + intMainintargcChar Const*argv[]) A { at inta,b,c,t; - //freopen ("Input.txt", "R", stdin); - intK; - while(SCANF ("%d", &k)! =EOF) { - while(k--) { -scanf"%d %d%d%d",&a,&b,&c,&t); in for(inti =0; i < A; i++) { - for(intj =0; J < b; J + +) { to for(intK =0; K < C; k++) { +scanf"%d",&cubic[i][j][k]); - } the } * } $ while(!Que.empty ()) {Panax Notoginseng Que.pop (); - } thememset (tcnt,-1,sizeof(tcnt)); +Point O (0,0,0,0); A Que.push (O); the intAns =-1; + while(!Que.empty ()) { -Point P =Que.front (); Que.pop (); $ intT0 =p.t; $ if(T0 >t) { - Continue; - } the if(p.x = =-1&& P.Y = = b1&& P.z = = C1) { -Ans =t0;Wuyi Break; the } - for(inti =0; I <6; i++) { Wu intXT = p.x + dir[i][0]; - intYT = p.y + dir[i][1]; About intZT = p.z + dir[i][2]; $ inttt = t0 +1; - if(XT < a && XT >=0&& YT < b && YT >=0&& ZT < c && ZT >=0&& Cubic[xt][yt][zt] = =0&& Tcnt[xt][yt][zt] = =-1) { - Point Toadd (XT,YT,ZT,TT); - Que.push (toadd); ATCNT[XT][YT][ZT] =tt; + } the } - } $printf"%d\n", ans); the } the } the return 0; the}