//注意输入的是列、行、层。//HDU1240 #include #include #include #include #include #include #include using namespace std; #define inf 10000000 char map[11][11][11]; int n; int dir[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}}; int fx[6]={1,-1,0,0,0,0},fy[6]={0,0,1,-1,0,0},fz[6]={0,0,0,0,-1,1}; int visit[11][11][11]; struct node { int x,y,z,step; }; int pd(int x,int y,int z) { if(x>=0&&x=0&&y=0&&z &&visit[x][y][z]==0&&map[x][y][z]=='O') return 1; return 0; } int bfs(int a,int b,int c,int d,int e,int f)//广度优先搜索 { int dx,dy,dz,i; queueq; memset(visit,0,sizeof(visit)); node front,rear; front.x=a,front.y=b,front.z=c,front.step=0; q.push(front); visit[a][b][c]=1; if(a==d&&b==e&&c==f) { return front.step; } while(!q.empty())//判断队列是否为空 { front=q.front();//队首元素出队 q.pop(); for(i=0;i<6;i++) { dx=front.x+dir[i][0]; dy=front.y+dir[i][1]; dz=front.z +dir[i][2]; if(pd(dx,dy,dz))//判断是否在图中且有无访问过 { visit[dx][dy][dz]=1;//标记当前点已访问过 if(dx==d&&dy==e&&dz==f)//判断当前点是否为终点 { return front.step+1; } rear.x=dx,rear.y=dy,rear.z=dz,rear.step=front.step+1;//步数为父节点(起始点)步数加1 q.push(rear); } } } return -1; } int main() { char s1[10],s2[10]; int a,b,c,d,e,f; while(~scanf("%s%d",s1,&n)) { int i,j,k; for(i=0;i for(j=0;j for(k=0;k cin>>map[i][j][k]; scanf("%d%d%d",&a,&b,&c); scanf("%d%d%d",&d,&e,&f); scanf("%s",s2); map[d][e][f]='O'; int ans=bfs(c,b,a,f,e,d);//注意输入转换; if(ans!=-1) printf("%d %d\n",n,ans); else printf("NO ROUTE\n"); } return 0; }