ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ - 2251 Dungeon Master 【bfs】

2022-07-01 11:03:05  阅读:134  来源: 互联网

标签:Dungeon ch const int bfs vis Master ans return


题目简述

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.

简析

\(\texttt{1.60pts}\)
dfs回溯搜索,因为矩阵是三阶形式,所以复杂度高达\(O(n^6)\),显然会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;
}

\(\texttt{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;
}

标签:Dungeon,ch,const,int,bfs,vis,Master,ans,return
来源: https://www.cnblogs.com/MrWangnacl/p/16433754.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有