在BFS中go数组的下标从1开始了!!其实应该从0开始啊,不知道我是怎么想的。
在三层循环赋值的时候把里层的k写成了j。。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct N{
int x,y,z,t;
};
int maze[51][51][51];//标志着是路还是墙
bool mark[51][51][51];//标志者已经走过了
queue<N> Q;
int go[][3]={
1,0,0,
-1,0,0,
0,1,0,
0,-1,0,
0,0,1,
0,0,-1
};
int bfs(int A,int B,int C)
{
while(Q.empty()==false)
{
N nowp=Q.front();
Q.pop();
int cx=nowp.x;
int cy=nowp.y;
int cz=nowp.z;
int ct=nowp.t;
for(int i=0;i<6;i++)
{
//居然在这里出错了!!一定要看清楚
int nx=cx+go[i][0];
int ny=cy+go[i][1];
int nz=cz+go[i][2];
int nt=ct+1;
if(nx<0 || nx>A-1 || ny<0 || ny>B-1 || nz<0 || nz>C-1 || maze[nx][ny][nz]==1 ||mark[nx][ny][nz]==1)continue;
N tmp;
tmp.x=nx;
tmp.y=ny;
tmp.z=nz;
tmp.t=nt;
Q.push(tmp);
mark[nx][ny][nz]=1;
if(nx==A-1 && ny==B-1 && nz==C-1)return nt;
}
}
return -1;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int A,B,C,T;
scanf("%d %d %d %d",&A,&B,&C,&T);
for(int i=0;i<A;i++)
{
for(int j=0;j<B;j++)
{
for(int k=0;k<C;k++)//傻缺错误,以后打代码睁着眼打!
{
scanf("%d",&maze[i][j][k]);
mark[i][j][k]=0;
}
}
}
mark[0][0][0]=1;
while(Q.empty()==false)Q.pop();
N tmp;
tmp.x=0;
tmp.y=0;
tmp.z=0;
tmp.t=0;
Q.push(tmp);
int ans=bfs(A,B,C);
if(ans<=T)printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}