POJ - 2251 Dungeon Master 【bfs】

题目简述

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

输入

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

简析

1.60pts
dfs回溯搜索,因为矩阵是三阶形式,所以复杂度高达O(n6),显然会TLE。
dfs部分代码:

void dfs(int x,int y,int z,int ans){
    if(z==C) y++,z=0;
    if(y==R) y=0,x++;
    if(x==L) return;
    if(ch[x][y][z]=='E'){
        Ans=min(Ans,ans);
        return;
    }
    dfs(x,y+1,z,ans);
    if(ch[x][y][z]!='#'&&!vis[x][y][z]){
        vis[x][y][z]=1;
        dfs(x,y+1,z,ans+1);
        vis[x][y][z]=0;
    }
    return;
}

2.100pts
dfs回溯复杂度高,显然是因为每一个回溯形成的子树深度太大,故而跑bfs的效率更高。
定义3个6维的方向变量,定义如下:

const int dx[]={-1,0,0,0,0,1};
const int dy[]={0,1,-1,0,0,0};
const int dz[]={0,0,0,-1,1,0};

分别对应的是西、北、南、下、上、东6个方向。
注意输入有坑,其他的就比较模板了。

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define N 31
int L,R,C;
const int dx[]={-1,0,0,0,0,1};
const int dy[]={0,1,-1,0,0,0};
const int dz[]={0,0,0,-1,1,0};
char ch[N][N][N];
bool vis[N][N][N],flag=0;
struct node{
    int x,y,z,ans;
};
void bfs(int x,int y,int z){
    queue<node> q;
    q.push({x,y,z,0});
    vis[x][y][z]=1;
    while(!q.empty()){
        node cur=q.front();
        q.pop();
        if(ch[cur.x][cur.y][cur.z]=='E'){
            flag=1;
            printf("Escaped in %d minute(s).\n",cur.ans);
            return;
        }
        for(int i=0;i<=5;i++){
            int nx=cur.x+dx[i],ny=cur.y+dy[i],nz=cur.z+dz[i];
            if(nx<0 || nx>=L) continue;
            if(ny<0 || ny>=R) continue;
            if(nz<0 || nz>=C) continue;
            if(vis[nx][ny][nz]) continue;
            if(ch[nx][ny][nz]!='#'){
                vis[nx][ny][nz]=1;
                q.push({nx,ny,nz,cur.ans+1});
                //vis[nx][ny][nz]=0;
            }
        }
    }
    return;
}
int ip(){
    int x=0,w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
int main(){
    L=ip(),R=ip(),C=ip();
    while(L&&R&&C){
        flag=0;
        memset(vis,0,sizeof(vis));
        memset(ch,0,sizeof(ch));
        int ansL=-1,ansR=-1,ansC=-1;
        for(int i=0;i<L;i++)
            for(int j=0;j<R;j++)
                for(int k=0;k<C;k++){
                    cin>>ch[i][j][k];
                    if(ch[i][j][k]=='S') ansL=i,ansR=j,ansC=k;
                }     
        bfs(ansL,ansR,ansC);
        if(!flag) printf("Trapped!\n");
        L=ip(),R=ip(),C=ip();
    }
    return 0;
}
posted @   SxtoxA  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
12 13
点击右上角即可分享
微信分享提示