#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int gox[7]={0,1,-1,0,0,0,0};
const int goy[7]={0,0,0,1,-1,0,0};
const int goz[7]={0,0,0,0,0,1,-1};
bool maze[55][55][55];
bool visit[55][55][55];
int xx[130005],yy[130005],zz[130005],tt[130005];
int a,b,c,t,num;
void bfs(int n)
{
if (tt[n]>t)
{
printf("-1\n");
return;
}
if (xx[n]==a && yy[n]==b && zz[n]==c)
{
printf("%d\n",tt[n]);
return;
}
for (int i=1;i<=6;i++)
if (maze[xx[n]+gox[i]][yy[n]+goy[i]][zz[n]+goz[i]] &&
visit[xx[n]+gox[i]][yy[n]+goy[i]][zz[n]+goz[i]]==0)
{
num++;
xx[num]=xx[n]+gox[i];
yy[num]=yy[n]+goy[i];
zz[num]=zz[n]+goz[i];
tt[num]=tt[n]+1;
visit[xx[num]][yy[num]][zz[num]]=1;
//cout<<num<<' '<<xx[num]<<' '<<yy[num]<<' '<<zz[num]<<' '<<tt[num]<<endl;
}
bfs(n+1);
}
int main()
{
int m;
scanf("%d",&m);
while (m--)
{
//memset(maze,0,sizeof(maze));
memset(visit,0,sizeof(visit));
//memset(xx,0,sizeof(xx));
//memset(yy,0,sizeof(yy));
//memset(zz,0,sizeof(zz));
//memset(tt,0,sizeof(tt));
scanf("%d%d%d%d",&a,&b,&c,&t);
for (int i=1;i<=a;i++)
for (int j=1;j<=b;j++)
for (int k=1;k<=c;k++)
{
scanf("%d",&maze[i][j][k]);
maze[i][j][k]=!maze[i][j][k];
}
xx[1]=1;yy[1]=1;zz[1]=1;tt[1]=0;
num=1;visit[1][1][1]=1;
bfs(1);
}
return 0;
}
代码
只向前走,没有前后走,找下个点,标志visit=1;那么后退了。
HDU1253胜利大逃亡,Runtime Error(ACCESS_VIOLATION),求找错
for (int i=1;i<=6;i++)if (maze[xx[n]+gox[i]][yy[n]+goy[i]][zz[n]+goz[i]] &&visit[xx[n]+gox[i]][yy[n]+goy[i]][zz[n]+goz[i]]==0){num++;xx[num]=xx[n]+gox[i];yy[num]=yy[n]+goy[i];zz[num]=zz[n]+goz[i]...