简单三维宽搜题,水之!点击打开链接
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#define N 100
using namespace std;
int x1,y1,z1,x2,y2,z2,n;
char map[N][N][N];
int jump[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
string s;
struct node
{
int x,y,z,s;
}q,r,p;
void init()
{
int i,j,k;
memset(map,'\0',sizeof(map));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
getchar();
for(k=0;k<n;k++)
cin>>map[i][j][k];
}
}
cin>>z1>>y1>>x1>>z2>>y2>>x2;
//cout<<x1<<' '<<y1<<' '<<z1<<' '<<x2<<' '<<y2<<' '<<z2<<endl;
/*for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
cout<<map[i][j][k];
cout<<endl;
}
}*/
}
int is_b(node p)
{
if(p.x<0 || p.y<0 || p.z<0 || p.x>=n || p.y>=n
|| p.z>=n || map[p.x][p.y][p.z]=='X')
return 1;
return 0;
}
int bfs()
{
int i;
q.s=0;q.x=x1;
q.y=y1;q.z=z1;
map[x1][y1][z1]='X';
queue< node > que;
que.push(q);
while(!que.empty())
{
r=que.front();que.pop();
if(r.x==x2 && r.y==y2 && r.z==z2)
return r.s;
for(i=0;i<6;i++)
{
p.s=r.s+1;
p.x=r.x+jump[i][0];
p.y=r.y+jump[i][1];
p.z=r.z+jump[i][2];
if(is_b(p)) continue;
map[p.x][p.y][p.z]='X';
que.push(p);
}
}
return -1;
}
int main ()
{
//freopen("mg.txt","r",stdin);
while(cin>>s>>n)
{
init();
cin>>s;
getchar();
int w=bfs();
if(w==-1) cout<<"NO ROUTE"<<endl;
else cout<<n<<' '<<w<<endl;
}
return 0;
}