poj2251-bfs- Dungeon Master

51 篇文章 0 订阅
46 篇文章 0 订阅

题意:

立体图形,走迷宫,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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值