标签:

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
11
三维的地图, 所以用三维数组。
其他和简单的BFS是一样的,方向就加了上下两个方向。
代码:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define M 100
struct node{
int x,y,z,time;
};
int map[M][M][M],a,b,c,T;
int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int DFS(int x,int y,int z)
{
int i;
node t,temp;
queue<node>Q;
bool vis[M][M][M];
memset(vis,0,sizeof(vis));
t.x=x;t.y=y;t.z=z;t.time=0;
Q.push(t);
vis[t.x][t.y][t.z]=1;
while(!Q.empty())
{
t=Q.front();
Q.pop();
if(t.x==b-1 && t.y==c-1 && t.z==a-1)
{
return t.time;
}
else
{
for(i=0;i<6;i++)
{
temp.x=t.x+dis[i][0];
temp.y=t.y+dis[i][1];
temp.z=t.z+dis[i][2];
temp.time=t.time+1;
//printf(" %d %d %d",temp.x,temp.y,temp.z);
if(temp.x>=0 &&temp.x<b && temp.y>=0 && temp.y<c && temp.z>=0 && temp.z<a)
{//printf("ok\n");
if(!vis[temp.x][temp.y][temp.z] && map[temp.x][temp.y][temp.z]==0)
{
vis[temp.x][temp.y][temp.z]=1;
Q.push(temp);
}
}
}
}
}
return 9999;
}
int main()
{
int i,j,k,C;
while(scanf("%d",&C)!=EOF)
{
while(C--)
{
scanf("%d%d%d%d",&a,&b,&c,&T);
for(k=0;k<a;k++)
for(i=0;i<b;i++)
for(j=0;j<c;j++)
scanf("%d",&map[i][j][k]);
i=DFS(0,0,0);
if(i>T) printf("-1\n");
else printf("%d\n",i);
}
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/qq2256420822/article/details/38166789