【POJ2251】Dungeon Master

本题传送门

本题知识点:宽度优先搜索

题意简单。在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径。每走一格每上或者下一层都算1步。

一开始以为这个“立体迷宫”有点吓到我(题做得太少了),后来发觉,只是一个三维数组以及多了两个操作方向(原地向上或者原地向下),除此之外就是简单的bfs模板题了。

数据很小。

// POJ 2251
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

// R == H, C == W
int L, R, C;
char maze[35][35][35];
int len[35][35][35];
bool stay[35][35][35];
int bl, br, bc, el, er, ec;
int rl[] = { 0, 0, 0, 0, 1, -1 };
int rc[] = { 1, -1, 0, 0, 0, 0 };
int rr[] = { 0, 0, 1, -1, 0, 0 };

struct node{
    int l, r, c;
};
queue<node> que;

void build(){
    for(int i = 0; i < L; i++){
            for(int j = 0; j < R; j++){
                scanf("%s", maze[i][j]);
                // 找起始点
                for(int k = 0; k < C; k++){
                    if(maze[i][j][k] == 'S'){
                        bl = i; br = j; bc = k;
                    }
                    if(maze[i][j][k] == 'E'){
                        el = i; er = j; ec = k;
                    }
                }
            }
        }
    // 注意队列一定要清空!
    while(!que.empty()) que.pop();
}

void show(){
    cout << "len\n";
    for(int i = 0; i < L; i++){
        for(int j = 0; j < R; j++){
            for(int k = 0; k < C; k++){
                printf("%d ", len[i][j][k]);
            } cout << endl;
        } cout << endl;
    } cout << endl;
}

void bfs(){
    node a;
    a.l = bl; a.r = br; a.c = bc;
    len[bl][br][bc] = 0;
    stay[bl][br][bc] = true;
    que.push(a);

    while(!que.empty()){
        node now = que.front(), next; que.pop();

        if(now.l == el && now.r == er && now.c == ec){
            break;
        }

        // 这里比较写的有点花
        for(int i = 0; i < 6; i++){
            next.l = now.l + rl[i]; next.r = now.r + rr[i]; next.c = now.c + rc[i];
            if(0 <= next.l && next.l < L && 0 <= next.r && next.r < R && 0 <= next.c && next.c < C
               && maze[next.l][next.r][next.c] != '#' && !stay[next.l][next.r][next.c]){
                    stay[next.l][next.r][next.c] = true;
                    len[next.l][next.r][next.c] = len[now.l][now.r][now.c] + 1;
                    que.push(next);
               }
        }
    }
}

int main()
{
    while(scanf("%d %d %d", &L, &R, &C) && L + R + C != 0){
        memset(stay, false, sizeof(stay));
        memset(len, -1, sizeof(len));
        build();

        // bfs
        bfs();

//        show();
        if(len[el][er][ec] != -1)
            printf("Escaped in %d minute(s).\n", len[el][er][ec]);
        else printf("Trapped!\n");
    }
    return 0;
}

//3 3 3
//S.#
//###
//###
//
//#.#
//###
//###
//
//#E#
//###
//###

原文地址:https://www.cnblogs.com/Ayanowww/p/11553190.html

时间: 2024-11-06 09:26:24

【POJ2251】Dungeon Master的相关文章

【LeetCode】Dungeon Game 解题报告【Solution】

[题目] The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight h

【SaltStack】在Master上给Minion端安装zabbix

一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和Minion的安装这里不再赘述! [配置Master] 1.新建目录 mkdir  -p  /srv/salt/iso mkdir -p   /srv/salt/install_zabbix 2.新建配置文件 (1)  /srv/top.sls 1 base: 2 '*': 3 - cpitable

【leetcode】Dungeon Game (middle)

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his wa

【leetcode】Dungeon Game

Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must

MFS分布式文件系统【2】MFS MASTER 部署

MFS版本 mfs-1.6.27 MFS-MASTER 192.168.1.190 MFS-CHUNKSERVER1 192.168.1.252 MFS-CHUNKSERVER2 192.168.1.188 MFS-client 192.168.1.52 下载MFS地址:http://sourceforge.net/projects/moosefs/files/moosefs/ 主控服务器MFS-MASTER部署 [[email protected]_190 mfs-1.6.27]# mkdir

【动态规划】Dungeon Game

题目:leetcode Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left r

POJ2251—— BFS——Dungeon Master

http://poj.org/problem?id=2251 /* 简单bfs,向六个方向拓展 */ /************************************************ * Author :Powatr * Created Time :2015-8-9 9:23:15 * File Name :B.cpp ************************************************/ #include <cstdio> #include &l

【POJ】1064Cable master

白皮书的二分 题意:有n条绳子,他们的长度分别为li,从他们中切个出k条长度相同的绳子的话,这k条绳子每条最长能有多长?保留小数点后两位 分析:二分 #include<cstdio> #include<cstring> #include<cmath> #define maxn 10010 #define INF 100001 double a[maxn]; int n,k; bool dix(double x) { int num=0,i; for(i=0;i<n

【总结】初创公司用AWS搭建高扩展性架构

下载地址:完整mp4视频 演讲人:张侠 博士 1. 邱洋的理解 初创公司需要快.多.好.省的技术架构 快:针对业务需要可以快速获得资源与服务 多:拥有丰富的云服务可供选择,能不自己做就不自己做 好:强调扩展性和高可用,既不要在一开始被"钱"束缚住,又需要良好的用户体验(能用是最基本的用户需求) 省:可以弹性伸缩,并按需付费是最好的节省 无论是初创公司还是传统企业,很多架构思路是相通的: OS.前端.后端.数据库.框架等,根据自身需要选择.之后要做的就是在云中找到对应的服务功能. 云应用