poj 2251 三维广搜

 这是自己做的第一道三维数组的题,刚开始看到以为很难,其实是十分的简单,不过十分悲催的是自己提交wa了很多次;

但是自己的数据没错啊,事后仔细想想,更改了数据,发现了问题所在,原来是没有清空队列,通过这件事,给了我

一个大教训,以后一定注意!!

#include<iostream>
#include<queue>
#include<cstring>
//#include<fstream>
using namespace std;


int L,R,C,mark,sum;
int bmp[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//六个搜索方向
int visited[35][35][35];//标记数组
char map[35][35][35];

struct node{
int x;
int y;
int z;
int temp;
};

bool cheak(int a,int b,int c) //搜索检查是否在图内
{
if(a >= 1 && a <= L && b >= 1 && b <=R && c >= 1 && c <= C)
return true;
return false;
}
node first,nexta,top;
queue<node> vi;
void bfs(int a,int b,int c)   //搜索函数
{
while(!vi.empty())//一定记住清空队列
vi.pop();
mark = 0;
first.x = a;
first.y = b;
first.z = c;
first.temp = 0;
vi.push(first);
visited[first.x][first.y][first.z] = 1;

while(!vi.empty())
{
top = vi.front();
vi.pop();

for(int i=0;i<6;i++){

nexta.x = top.x + bmp[i][0];
nexta.y = top.y + bmp[i][1];
nexta.z = top.z + bmp[i][2];
nexta.temp = top.temp;
if(cheak(nexta.x,nexta.y,nexta.z) && !visited[nexta.x][nexta.y][nexta.z] && map[nexta.x][nexta.y][nexta.z] != '#')
{
if(map[nexta.x][nexta.y][nexta.z] == '.' )
nexta.temp ++;
if(map[nexta.x][nexta.y][nexta.z] == 'E')
{
mark = 1;
sum = nexta.temp+1;
return;
}
visited[nexta.x][nexta.y][nexta.z] = 1;
vi.push(nexta);

}
}
}
}
int main()
{
//ifstream cin("in.txt");
while(cin>>L>>R>>C,L || R || C)
{
memset(visited,0,sizeof(visited));
for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++){
for(int k=1;k<=C;k++)
cin>>map[i][j][k];
}
}
for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++){
for(int k=1;k<=C;k++)
if(map[i][j][k] == 'S')
bfs(i,j,k);
}
}
if(mark)
cout<<"Escaped in "<<sum<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
}

原文地址:https://www.cnblogs.com/lfyy/p/2748209.html