【牛客小白月赛21】NC201613 Jelly 题目链接
三维bfs走迷宫
#include<cstdio> #include<iostream> #include<queue> using namespace std; int n,nx[6]={1,-1,0,0,0,0},ny[6]={0,0,1,-1,0,0},nz[6]={0,0,0,0,1,-1}; char c[105][105][105]; bool vis[105][105][105]; struct node{ int x,y,z,s; }; queue<node> q; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) for (int k=1;k<=n;k++) cin>>c[i][j][k]; if (c[n][n][n]=='*') return puts("-1")&0; q.push((node){ 1,1,1,1 }),vis[1][1][1]=1; while(!q.empty()){ node a=q.front(); q.pop(); for (int i=0,xx,yy,zz;i<6;i++){ xx=a.x+nx[i],yy=a.y+ny[i],zz=a.z+nz[i]; if (vis[xx][yy][zz] || c[xx][yy][zz]=='*' || xx<1 || yy<1 || zz<1 || xx>n || yy>n || zz>n) continue; q.push((node){ xx,yy,zz,a.s+1 }),vis[xx][yy][zz]=1; if (xx==n && yy==n && zz==n) return printf("%d",a.s+1)&0; } } puts("-1"); }