题意:一个nnn的三维空间,给出起点坐标和终点坐标,求最小步数。
题记:简单BFS,需要注意的是我们建图的坐标和输入的坐标是不一样的,需要处理一下。
#include<bits/stdc++.h>
using namespace std;
const int N=15;
char a[N][N][N];
int dis[6][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
int x1,y1,z1,x2,y2,z2,n;
struct node{
int x,y,z;
int t;
};
bool check(int x,int y,int z){
if(x<0||x>=n||y<0||y>=n||z<0||z>=n||a[x][y][z]=='X')
return false;
else
return true;
}
int bfs(){
queue<node>q;
node st,ne;
st.x=x1;
st.y=y1;
st.z=z1;
st.t=0;
q.push(st);
if(st.x==x2&&st.y==y2&&st.z==z2)
return st.t;
while(!q.empty()){
st=q.front();
q.pop();
for(int i=0;i<6;i++){
ne.x=st.x+dis[i][0];
ne.y=st.y+dis[i][1];
ne.z=st.z+dis[i][2];
ne.t=st.t+1;
if(ne.x==x2&&ne.y==y2&&ne.z==z2)
return ne.t;
if(check(ne.x,ne.y,ne.z)){
a[ne.x][ne.y][ne.z]='X';
q.push(ne);
}
}
}
return -1;
}
int main(){
string s;
while(cin>>s){
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
cin>>a[i][j][k];
cin>>y1>>z1>>x1>>y2>>z2>>x2>>s;
int ans;
ans=bfs();
if(ans!=-1) cout<<n<<' '<<ans<<endl;
else cout<<"NO ROUTE"<<endl;
}
return 0;
}