简单bfs,找两点之间的最短路径
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
char graph[20][20][20];
bool vis[20][20][20];
int n,sx,sy,sz,ex,ey,ez;
int dx[] = {1,-1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {0,0,0,0,1,-1};
struct node{
int x,y,z,step;
};
bool judge(int x,int y,int z){
if(x<0 || y<0 || z<0 || x>=n || y>=n || z>=n || vis[x][y][z])
return false;
return true;
}
int bfs(){
queue<node> q;
node cur,next;
cur.x = sx;
cur.y = sy;
cur.z = sz;
cur.step = 0;
vis[sx][sy][sz] = 1;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.x==ex && cur.y==ey && cur.z==ez){
return cur.step;
}
for(int i=0;i<6;i++){
next = cur;
next.x += dx[i];
next.y += dy[i];
next.z += dz[i];
if(judge(next.x,next.y,next.z)){
next.step++;
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
}
return -1;
}
int main(){
char s[10];
while(scanf("%s%d",s,&n)!=EOF){ //START N
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%s",graph[i][j]);
scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);
scanf("%s",s); //END
int res = bfs();
if(res>=0)
printf("%d %d\n",n,res);
else
printf("NO ROUTE\n");
}
return 0;
}