一个三维数组要你求 给定的开始点和终点 最小的 步数 所以用 广搜;
第一建立一个三维图;从给定的开始点 往六个方向进行广搜;如果满足条件就跳出;
最好不要用cin输入
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[10][10][10];
int n, st1, st2, st3, en1, en2, en3, flag;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
struct node
{
int x;
int y;
int z;
int step;
};
void bfs(int st1, int st2, int st3)
{
queue<node>q;
node temp, type;
temp.x = st1;//初始化;
temp.y = st2;
temp.z = st3;
temp.step = 0;
q.push(temp);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.x == en1 && temp.y == en2 && temp.z == en3)//判断是否符合条件;
{
flag = temp.step;
return ;
}
for(int i = 0; i < 6; i++)//将六个方向的元素放到队列中;
{
int fx = temp.x + dir[i][0];
int fy = temp.y + dir[i][1];
int fz = temp.z + dir[i][2];
if(fx < 0 || fx >= n || fy < 0 || fy >= n || fz < 0 || fz >= n || map[fx][fy][fz] == 'X')
{
continue;
}
type.x = fx;
type.y = fy;
type.z = fz;
type.step = temp.step + 1;
map[fx][fy][fz] = 'X';//标记防止在被使用;
q.push(type);
}
}
return ;
}
int main()
{
char str[10];
while(scanf("%s %d", str, &n)==2)//如果没有==2 则会超时;注意啊!!!
{
for(int k=0;k<n;k++)//建图;
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<n;j++)
{
scanf("%c",&map[i][j][k]);
}
}
scanf("%d %d %d",&st1,&st2,&st3);
scanf("%d %d %d",&en1,&en2,&en3);
scanf("%s",str);
map[st1][st2][st3] = 'X';//标记 防止在被使用;
flag = -1;
bfs(st1, st2, st3);
if(flag == -1)
printf("NO ROUTE\n");
else
printf("%d %d\n", n, flag);
}
}