The concept of BFS in a three-dimensional matrix is quite simple.
However, I did not expect this during the graph creation process.
As a result, it was WA for four times ,,,,,,
Note that this is correct.
For (int I = 1; I <= n; I ++)
For (int k = 1; k <= n; k ++)
For (int p = 1; p <= n; p ++)
Cin> map [p] [k] [I];
This is not the case.
For (int I = 1; I <= n; I ++)
For (int k = 1; k <= n; k ++)
For (int p = 1; p <= n; p ++)
Cin> map [I] [k] [p];
The triplicate I, k, and p represent the Z axis, Y axis, and X axis of the Space Cartesian coordinate system.
#include<cstdio> #include<iostream>#include<cstring>#include <cstdlib>#include<queue>using namespace std;const int N=15 ;typedef struct node{int x,y,z,step;}coord;coord s,e;bool used[N][N][N];char map[N][N][N], str[10];int n,dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};void gchar( char &c ){while( cin>>c, c!= 'O'&& c!= 'X' ) ;}inline int isend(coord p){if(p.x==e.x&&p.y==e.y&&p.z==e.z)return 1;elsereturn 0;}int BFS(){memset(used,0,sizeof(used));s.step=0;used[s.x][s.y][s.z]=1;coord now,next;queue<coord>Q;Q.push(s);while(!Q.empty()){now=Q.front();Q.pop();if( now.x== e.x&& now.y== e.y&& now.z== e.z )return now.step;for(int i=0;i<6;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];next.z=now.z+dir[i][2];next.step=now.step+1;if(map[next.x][next.y][next.z]== 'O'&& !used[next.x][next.y][next.z]){used[next.x][next.y][next.z]=1;Q.push(next);}}}return -1;}int main (){while(cin>>str>>n){memset(map,0,sizeof(map));for(int i=1;i<=n;i++)for(int k=1;k<=n;k++)for(int p=1;p<=n;p++)gchar(map[p][k][i]);cin>>s.x>>s.y>>s.z>>e.x>>e.y>>e.z>>str;s.x+= 1, s.y+= 1, s.z+= 1, e.x+= 1, e.y+= 1, e.z+= 1;int ans=BFS();if(ans>=0)printf("%d %d\n",n,ans);elseprintf("NO ROUTE\n");}return 0;}
,