POJ 2251 Dungeon Master

链接:http://poj.org/problem?id=2251


题意:走3D迷宫,若能走出则输出走了几步


BFS,三维数组


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char map[31][31][31];
int L,R,C,ans,dir[6][3]= {-1,0,0, 1,0,0, 0,-1,0, 0,1,0, 0,0,1, 0,0,-1};
typedef struct node {
    int x,y,z,dis;
} node;
node st;
bool noway(int x,int y,int z) {
    return map[st.z-1][st.x][st.y]=='#' && map[st.z+1][st.x][st.y]=='#' && map[st.z][st.x-1][st.y]=='#' && map[st.z][st.x+1][st.y]=='#'
           && map[st.z][st.x][st.y-1]=='#' && map[st.z][st.x][st.y+1]=='#';
}
bool isin(int x,int y,int z) {
    return x>=0 && x<R && y<C && y>=0 && z<L && z>=0;
}
int Bfs() {
    queue<node> q;
    q.push(st);
    while(!q.empty()) {
        node cur;
        cur=q.front();
        q.pop();
        for(int i=0; i<6; i++) {
            node tmp=cur;
            tmp.x+=dir[i][0];
            tmp.y+=dir[i][1];
            tmp.z+=dir[i][2];
            if(isin(tmp.x,tmp.y,tmp.z) && map[tmp.z][tmp.x][tmp.y]!='#') {
                tmp.dis++;
                if(map[tmp.z][tmp.x][tmp.y]=='E') return tmp.dis;
                map[tmp.z][tmp.x][tmp.y]='#';
                q.push(tmp);
            }
        }
    }
    return 0;
}
int main() {
    while(scanf("%d %d %d",&L,&R,&C),L+R+C) {
        memset(map,0,sizeof(map));
        for(int i=0; i<L; i++) {
            for(int j=0; j<R; j++) {
                getchar();
                for(int k=0; k<C; k++) {
                    scanf("%c",&map[i][j][k]);
                    if(map[i][j][k]=='S') {
                        st.z=i;
                        st.x=j;
                        st.y=k;
                        st.dis=0;
                        map[i][j][k]='#';
                    }
                }
            }
            getchar();
        }
        if(noway(st.x,st.y,st.z)) {
            printf("Trapped!\n");
            continue;
        }
        ans=Bfs();
        printf(!ans ? "Trapped!\n" : "Escaped in %d minute(s).\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值