G——胜利大逃亡 (BFS)

例题都过不了 原因不详

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int mp[55][55][55];
bool vis[55][55][55];
int r[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct weizhi
{
int x,y,z,step;
};
weizhi a,next;
int A,B,C,K,ans;
bool judge(int x,int y,int z)
{
if(x<0|y<0|z<0|x>=A|y>=B|z>=C)
return 0;
if(mp[x][y][z]=='1')
return 0;
if(vis[x][y][z]==1)
return 0;
return 1;
}
int BFS()
{
int i,X,Y,Z,S;
queue<weizhi>Q;
a.x=0;a.y=0;a.z=0;
a.step=0;
Q.push(a);
vis[0][0][0]=true;
while(Q.empty())
{
a=Q.front();
Q.pop();
if(a.x==A-1&&a.y==B-1&&a.z==C-1)
{
return a.step;
}
for(i=0;i<6;i++)
{
X=a.x+r[i][0];
Y=a.y+r[i][1];
Z=a.z+r[i][2];
S=a.step+1;
if(judge(X,Y,Z))
{
vis[X][Y][Z]=true;
next.x=X; next.y=Y; next.z=Z; next.step=S;
Q.push(next);
}
}
}
return 0;

}
int main()
{
int i,j,t,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&A,&B,&C,&K);
// memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
for(i=0;i<A;i++)
{
for(j=0;j<B;j++)
{
for(k=0;k<C;k++)
{
scanf("%d",&mp[i][j][k]);
}
}
}
// for(i=0;i<A;i++)
// {
// for(j=0;j<B;j++)
// {
// for(k=0;k<C;k++)
// {
// printf("%d ",mp[i][j][k]);
// }
// cout<<' ';
// }
// }
ans=0;
ans=BFS();
if(ans<K) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}