题意:
立体图形,走迷宫,S到E
思路:
模板bfs。标记。。标记。。标记。。
#include <iostream>
#include <queue>
#include <stdio.h>
#include <cstring>
using namespace std;
struct node
{
int x,y ,z,step;
};
queue <node > s;
char mp[35][35][35];
int vis[35][35][35];
int a,b,c;
int sum=0;
void bfs()
{
while(!s.empty())
{
node f=s.front();
s.pop();
if(mp[f.z][f.x][f.y]=='E')
{
printf("Escaped in %d minute(s).\n",f.step);
return ;
}
int next[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
for(int i=0;i<6;i++)
{
node temp=f;
temp.x+=next[i][0];
temp.y+=next[i][1];
temp.z+=next[i][2];
if(vis[temp.z][temp.x][temp.y]==1||temp.z>=a||temp.x>=b||temp.y>=c||temp.z<0||temp.x<0||temp.y<0||mp[temp.z][temp.x][temp.y]=='#')
continue;
vis[temp.z][temp.x][temp.y]=1;
temp.step++;
s.push(temp);
}
}
printf("Trapped!\n");
return ;
}
int main()
{
while(~scanf("%d %d %d",&a,&b,&c))
{
memset(vis,0,sizeof(vis));
while(!s.empty())
s.pop();
int flag=0;
if(a==0&&b==0&&c==0)
break;
for(int i=0;i<a;i++)
{
for(int row=0;row<b;row++)
{
scanf("%s",&mp[i][row]);
}
}
node t;
for(int i=0;i<a;i++)
{
for(int row=0;row<b;row++)
{
for(int col=0;col<c;col++)
{
if(mp[i][row][col]=='S')
{
t.z=i;
t.x=row;
t.y=col;
}
if(mp[i][row][col]=='E')
{
flag=1;
}
// printf("%c",mp[i][row][col]);
}
// cout<<endl;
}
// cout<<endl;
}
if(flag==0)
{
printf("Trapped!\n");
continue;
}
vis[t.z][t.x][t.y]=1;
t.step=0;
s.push(t);
bfs();
}
return 0;
}