Not happy, simple three-dimensional BFS
#include <iostream> #include <queue>using namespace std;string str1,str2;int n;int Sx,sy,sz,ex,ey,ez;char Mapp[11][11][11];int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}; int re;struct stu{int x,y,z,s;}; void BFs () {Stu x,y;queue<stu>root;x.x=sx;x.y=sy;x.z=sz;x.s=0; Root.push (x); mapp[x.z][x.x][x.y]= ' x '; while (Root.size ()) {X=root.front ();//cout<< "<<x.x<<" <<x.y<< ' <<x.z<<endl; Root.pop (); if (X.x==ex&&x.y==ey&&x.z==ez) {Re=x.s;return;} for (int i=0;i<6;i++) {y.x=x.x+dir[i][0];y.y=x.y+dir[i][1];y.z=x.z+dir[i][2];y.s=x.s+1;if (y.x<0| | y.x>=n| | y.y<0| | y.y>=n| | y.z<0| | y.z>=n| | mapp[y.z][y.x][y.y]== ' x ') {continue;} Root.push (y); mapp[y.z][y.x][y.y]= ' x ';}} }int Main () {while (cin>>str1>>n) {for (int. i=0;i<n;i++) {for (int. j=0;j<n;j++) {for (int k=0;k<n;k++) { {cin>>mapp[i][j][k];}}} CIN>>SX>>SY>>SZ>>EX>>EY>>EZ;CIN>>STR2;RE=-1;BFS (if (re!=-1) cout<<n<< "<<re<<endl;else cout<<" NO ROUTE "<<ENDL;} return 0;}
HDU 1240 Asteroids!