DESCRIPTION:
给你一个三维的迷宫。问你是否能从起点走到终点。如果能,输出最小步数。对我来说难得就是我没有想到怎么把他给你的三维图转换成map。恩。、好像解题报告上说。只要是这种的最短路都要用bfs。用dfs回很难。不太懂耶。>_<...

然后就是普通的bfs了。然后忘了三个输入全为0的时候结束程序。然后WA了一会。。然后就没有然后了。233333333333

附代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

int l, r, c;
int ans;
bool used[40][40][40];
bool map[40][40][40];

struct Node
{
    int l, r, c;
    int step;
    Node()
    {
        step = 0;
    }
}node[30000], now, temp, st, ed;

int move[6][3] = {0, 0, 1,  0, 0, -1,  0, 1, 0,  0, -1, 0,  1, 0, 0, -1, 0, 0};

bool check(int l, int r, int c)
{
    if (l >= 0 && l < 30 && r >= 0 && r < 30 && c >= 0 && c < 30 && !used[l][r][c] && map[l][r][c])
        return true;
    return false;
}

bool bfs(int i, int j, int k)
{
     int top = 0;
     int tail = 0;
     node[tail++] = st;
     used[st.l][st.r][st.c] = 1;
     while(top < tail)
     {
         now = node[top++];
         if (now.l == ed.l && now.r == ed.r && now.c == ed.c)
         {
             ans = now.step;
             return true;
         }
         for (int i=0; i<6; ++i)
         {
             temp.l = now.l + move[i][0];
             temp.r = now.r + move[i][1];
             temp.c = now.c + move[i][2];
             if (check(temp.l, temp.r, temp.c))
             {
                 temp.step = now.step + 1;
                 node[tail++] = temp;
                 used[temp.l][temp.r][temp.c] = true;
             }
         }
     }
     return false;
}

int main()
{
    char t;
    while(cin >> l)
    {
        cin >> r >> c;
        if (l == 0 && r == 0 && c == 0)
            break;
        memset(used, 0, sizeof(used));
        memset(map, 0, sizeof(map));
        for (int ll=0; ll<l; ++ll)
        {
            for (int rr=0; rr<r; ++rr)
            {
                for (int cc=0; cc<c; ++cc)
                {
                    cin >> t;
                   if (t == 'S')
                   {
                     map[ll][rr][cc] = true;
                     st.l = ll;
                     st.r = rr;
                     st.c = cc;
                     st.step = 0;
                   }
                   if (t == '.')
                    map[ll][rr][cc] = true;
                   if (t == 'E')
                   {
                       map[ll][rr][cc] = true;
                       ed.l = ll;
                       ed.r = rr;
                       ed.c = cc;
                   }
                }
            }
        }
        if (bfs(st.l, st.r, st.c))
            cout << "Escaped in " << ans << " minute(s)." << endl;
        else cout << "Trapped!\n";
    }
    return 0;
}

POJ 2251 bfs的更多相关文章

  1. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  2. POJ 2251 BFS(简单)

    一道三维的BFS Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24003 Accepted: 9 ...

  3. POJ - 2251 bfs [kuangbin带你飞]专题一

    立体bfs,共有六个方向: const int dx[] = {0,0,1,-1,0,0}; const int dy[] = {1,-1,0,0,0,0}; const int dz[] = {0, ...

  4. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. 【POJ 2251】Dungeon Master(bfs)

    BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...

  7. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

  8. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  9. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

随机推荐

  1. UVa 714 Copying Books - 二分答案

    求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...

  2. 自己写操作系统---bootsector篇

    其实博主本来想在寒假自己写一个OSkernal的,高高兴兴的影印了本<一个操作系统的实现>. 然后又去图书馆借来<30天自制操作系统>和<X86/X64体系探索编程> ...

  3. 委托的begininvoke

    http://blog.csdn.net/cml2030/article/details/2172854 http://blog.163.com/weizhiyong_111/blog/static/ ...

  4. OI无关--关于侧边栏

    自己在比较闲的时候学了一点html和js,大概能写一些比较简单的东西了,于是就动起了侧边栏的念头. 如果能在博客里加一个题目快速跳转也很兹磁啊. 首先要选择题目,oj的名字肯定是不能直接输入,因为还有 ...

  5. trigger自动执行事件

    html <button>点击文字变红</button> <p>trigger出发点击事件</p> js $('button').click(funct ...

  6. js文字转移效果

    这个例子算是有点样子的. 思路: 字符串操作.左框里面先是预设的.点击按钮时截取左框中的字符串的前一个字符到右框里的字符串后面,以此循环.点击按钮时按钮变为灰色,在循环完成后恢复.计数的总数(右边)是 ...

  7. UVa 11991 一道简单题

    https://vjudge.net/problem/UVA-11991 题意:给出一个包含n个整数的数组,你需要回答若干询问.每次询问两个整数k和v,输出从左到右第k个v的下标. 思路: 把每个数字 ...

  8. Spring/Spring MVC/Spring Boot的区别

    1.spring boot更简单,容易上手: 2.spring boot对第三方技术进行了很好的封装,提供了大量的第三方接口: 3.通过依赖配置,不需要XML等配置文件: 4.提供了安全性等特性. S ...

  9. Java 注解的概念与种类

    Java 注解的概念与种类 一,什么是注解 注解和XML文件都是常用的,对web项目进行配置性描述的方式. 举个最简单的例子,对于一个Servlet,比如LoginServlet,采用如下方式: @W ...

  10. 雷林鹏分享:C# 预处理器指令

    C# 预处理器指令 预处理器指令指导编译器在实际编译开始之前对信息进行预处理. 所有的预处理器指令都是以 # 开始.且在一行上,只有空白字符可以出现在预处理器指令之前.预处理器指令不是语句,所以它们不 ...