POJ2251(三维BFS)

水题。 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=35;
const double eps=1e-8;
const double PI = acos(-1.0);
int g[35][35][35],vis[35][35][35];
struct node
{
    int x,y,z,step;
};
int x,y,z;
node s,e;
int dx[]={0,1,0,0,0,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,0,0,0,-1,0};
int bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node> q;
    q.push(s);
    int ans=0;
    while(!q.empty())
    {
        node t=q.front();
       // cout<<t.step<<endl;
        if(t.x==e.x&&t.y==e.y&&t.z==e.z)
        {
            ans=t.step;
            break;
        }
        q.pop();
        int xx=t.x,yy=t.y,zz=t.z;
        for(int i=0;i<6;i++)
        {
            xx=t.x+dx[i];
            yy=t.y+dy[i];
            zz=t.z+dz[i];
            if(xx>0&&xx<=x&&yy>0&&yy<=y&&zz<=z&&zz>0&&!vis[zz][xx][yy]&&g[zz][xx][yy]==1)
            {
              //  cout<<"inf: "<<xx<<" "<<yy<<" "<<zz<<endl;
                node u;
                u.x=xx;
                u.y=yy;
                u.z=zz;
                u.step=t.step+1;
                q.push(u);
                vis[zz][xx][yy]=1;
            }
        }
    }
    return ans;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    while(cin>>z>>x>>y)
    {

        if(!x&&!y&&!z)
            break;
        for(int i=1;i<=z;i++)
        {
            for(int j=1;j<=x;j++)
            {
                for(int k=1;k<=y;k++)
                {
                    char c;
                    cin>>c;
                    if(c=='#')
                        g[i][j][k]=0;
                    else if(c=='.')
                        g[i][j][k]=1;
                    else if(c=='S')
                    {
                        g[i][j][k]=1;
                        s.x=j;
                        s.y=k;
                        s.z=i;
                        s.step=0;
                    }
                    else if(c=='E')
                    {
                        g[i][j][k]=1;
                        e.x=j;
                        e.y=k;
                        e.z=i;
                        e.step=-1;
                    }
                }
            }
          //  getchar();
        }
      /*  for(int i=1;i<=z;i++)
        {
            for(int j=1;j<=x;j++)
            {
                for(int k=1;k<=y;k++)
                {
                    cout<<g[i][j][k]<<" ";
                }
                cout<<endl;
            }
            cout<<endl<<endl;
        }*/
        int ans=bfs();
        if(!ans)
            cout<<"Trapped!"<<endl;
        else
            cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值