poj 2251

  还是迷宫的问题。不过这次是三维的,代码很快写完了。但是有个问题搞错了。。三维数组存矩阵的时候,顺序弄错了 悲剧啊。

 

、#include <iostream>
#include <fstream>
#include <cstdio>
#include <queue>
#include <memory.h>

#define MAX 31

using namespace std;

struct node
{
int x,y,z;
int step;
node(){}
node(int x1,int y1,int z1,int s)
{
x=x1; y=y1 ; z=z1; step=s;

}
};
int dirx[MAX]={1,-1,0,0,0,0};
int diry[MAX]={0,0,1,-1,0,0};
int dirz[MAX]={0,0,0,0,1,-1};
bool visit[MAX][MAX][MAX];
int map[MAX][MAX][MAX];
char s[MAX];
int l,r,c;
int start[3],end[3];

void getmap()
{
for(int k=0 ; k<l; k++)
{
for(int i=0; i<r; i++)
{
getchar();
scanf("%s",s);
for(int j=0; j<c; j++)
{
if(s[j]=='S')
{
start[0]=k;
start[1]=i;
start[2]=j;
map[k][i][j]=1;
}
else if(s[j]=='E')
{
end[0]=k;
end[1]=i;
end[2]=j;
}
else if(s[j]=='#')
{
map[k][i][j]=1;
}
}
}
getchar();
}
}

int bfs()
{
queue<node> q;
node n(start[0],start[1],start[2],0);
visit[start[0]][start[1]][start[2]]=true;
q.push(n);
while(!q.empty())
{
node next=q.front();
q.pop();
for(int i=0; i<6; i++)
{
int temx=next.x+dirx[i];
int temy=next.y+diry[i];
int temz=next.z+dirz[i];
if(temx==end[0] && temy==end[1] && temz==end[2])
{
return next.step+1;
}
if(temx>=0 && temx<l && temy>=0 && temy <r && temz>=0 && temz<c &&
map[temx][temy][temz]!=1 && !visit[temx][temy][temz])
{
visit[temx][temy][temz]=true;
node temp(temx,temy,temz,next.step+1);
q.push(temp);
}
}
}
return -1;
}

int main()
{
freopen("acm.txt","r",stdin);
int result;
while(scanf("%d%d%d",&l,&r,&c) && (l+r+c))
{
memset(map,0,sizeof(map));
memset(visit,false,sizeof(visit));
getmap();
result=bfs();
if(result==-1)
{
printf("Trapped!\n");
}
else
{
printf("Escaped in %d minute(s).\n",result);
}
}
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值