zoukankan      html  css  js  c++  java
  • (简单) POJ 2251 Dungeon Master,BFS。

      Description

      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.

      Is an escape possible? If yes, how long will it take?

      一个三维的迷宫问题,和二维没什么区别,直接BFS就好。

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    bool map1[40][40][40];
    bool vis[40][40][40];
    int L,R,C;
    int Sl,Sr,Sc,El,Er,Ec;
    
    struct state
    {
        int l,r,c;
        int num;
    
        state() {}
        state(int x,int y,int z,int n):l(x),r(y),c(z),num(n) {}
    };
    
    bool judge(int x,int y,int z)
    {
        if(x<=0||x>L||y<=0||y>R||z<=0||z>C)
            return 0;
    
        if(map1[x][y][z]==0)
            return 0;
    
        if(vis[x][y][z])
            return 0;
    
        vis[x][y][z]=1;
        return 1;
    }
    
    int bfs()
    {
        queue <state> que;
        state temp;
        int tl,tr,tc;
    
        que.push(state(Sl,Sr,Sc,0));
    
        while(!que.empty())
        {
            temp=que.front();
            que.pop();
    
            if(temp.l==El&&temp.r==Er&&temp.c==Ec)
                return temp.num;
    
            tl=temp.l;
            tr=temp.r;
            tc=temp.c;
    
            if(judge(tl-1,tr,tc))
                que.push(state(tl-1,tr,tc,temp.num+1));
            if(judge(tl+1,tr,tc))
                que.push(state(tl+1,tr,tc,temp.num+1));
            if(judge(tl,tr-1,tc))
                que.push(state(tl,tr-1,tc,temp.num+1));
            if(judge(tl,tr+1,tc))
                que.push(state(tl,tr+1,tc,temp.num+1));
            if(judge(tl,tr,tc-1))
                que.push(state(tl,tr,tc-1,temp.num+1));    
            if(judge(tl,tr,tc+1))
                que.push(state(tl,tr,tc+1,temp.num+1));
        }
    
        return -1;
    }
    
    int main()
    {
        char s[50];
        int ans;
    
        ios::sync_with_stdio(false);
    
        while(cin>>L>>R>>C)
        {
            memset(vis,0,sizeof(vis));
    
            if(!L&&!R&&!C)
                break;
    
            for(int i=1;i<=L;++i)
                for(int j=1;j<=R;++j)
                {
                    cin>>s;
                    for(int k=1;k<=C;++k)
                        switch(s[k-1])
                        {
                            case 'S':
                                Sl=i;
                                Sr=j;
                                Sc=k;
                                map1[i][j][k]=1;
                                break;
                            case 'E':
                                El=i;
                                Er=j;
                                Ec=k;
                                map1[i][j][k]=1;
                                break;
                            case '.':
                                map1[i][j][k]=1;
                                break;
                            case '#':
                                map1[i][j][k]=0;
                                break;
                        }
                }
    
            ans=bfs();
    
            if(ans!=-1)
                cout<<"Escaped in "<<ans<<" minute(s).
    ";
            else
                cout<<"Trapped!
    ";
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    开源作品-PHP写的Redis管理工具(单文件绿色版)-SuRedisAdmin_PHP_1_0
    开源作品-PHP写的JS和CSS文件压缩利器(单文件绿色版)-SuMinify_PHP_1_5
    Javascript关于JSON集合的几种循环方法
    正式安家
    Oracle 常用脚本
    Centos6安装zabbix_server
    centos_6下源码编译安装zabbix之一 LNMP环境的搭建
    reg51.h
    C语言浮点数存储结构分析
    WinForm程序中两份mdf文件问题的解决
  • 原文地址:https://www.cnblogs.com/whywhy/p/4228073.html
Copyright © 2011-2022 走看看