简单的BFS啦。。。。。
AC代码如下:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef struct{
int x, y, z;
int step;
}Node;
int moves[6][3] = { 0, 0, 1,
0, 0, -1,
0, 1, 0,
0, -1, 0,
1, 0, 0,
-1, 0, 0 };
int N;
bool mark[10][10][10];
int map[10][10][10];
Node start, endd;
int BFS(){
queue<Node> q;
start.step = 0;
q.push( start );
mark[start.z][start.y][start.x] = true;
while( !q.empty() ){
Node n = q.front();
q.pop();
if( n.x == endd.x && n.y == endd.y && n.z == endd.z ){
return n.step;
}
for( int i = 0; i < 6; i++ ){
Node temp = n;
temp.x += moves[i][0];
temp.y += moves[i][1];
temp.z += moves[i][2];
temp.step += 1;
if( mark[temp.z][temp.y][temp.x] == true ){
continue;
}
if( map[temp.z][temp.y][temp.x] == 1 ){
continue;
}
if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= N || temp.z < 0 || temp.z >= N ){
continue;
}
mark[temp.z][temp.y][temp.x] = true;
q.push( temp );
}
}
return -1;
}
int main(){
char s[20];
while( scanf( "%s%d", s, &N ) != EOF ){
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
scanf( "%s", s );
for( int k = 0; k < N ; k++ ){
if( s[k] == 'O' ){
map[i][j][k] = 0;
}else{
map[i][j][k] = 1;
}
}
}
}
cin >> start.x >> start.y >> start.z;
cin >> endd.x >> endd.y >> endd.z;
scanf( "%s", s );
memset( mark, false, sizeof( mark ) );
int ans = BFS();
if( ans >= 0 ){
cout << N << " " << ans << endl;
}else{
cout << "NO ROUTE" << endl;
}
}
return 0;
}