virtual judge(专题一 简单搜索 B)

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?

Input

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.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

三维迷宫水题
#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int MAXN=31;
char dun[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
struct node{
    int L,R,C;
    int step;
    node(int l,int r,int c,int s):L(l),R(r),C(c),step(s){}
    node(){}
};
int l,r,c;
int sl,sr,sc;
int el,er,ec;
int dl[6]={1,0,0,0,0,-1};
int dc[6]={0,1,0,-1,0,0};
int dr[6]={0,0,1,0,-1,0};
int bfs()
{
    queue<node> que;
    que.push(node(sl,sr,sc,0));
    vis[sl][sr][sc]=1;
    while(!que.empty())
    {
        node now=que.front();que.pop();
        if(now.L==el&&now.R==er&&now.C==ec)
        {
            return now.step;
        }
        for(int i=0;i<6;i++)
        {
            int nl=now.L+dl[i];
            int nr=now.R+dr[i];
            int nc=now.C+dc[i];
            if(0<=nl&&nl<l&&0<=nr&&nr<r&&0<=nc&&nc<c&&!vis[nl][nr][nc]&&dun[nl][nr][nc]!=‘#‘)
            {
                vis[nl][nr][nc]=1;
                que.push(node(nl,nr,nc,now.step+1));
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&l,&r,&c)!=EOF&&l!=0)
    {
        memset(vis,0,sizeof(vis));
        scanf("%*c");
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                for(int z=0;z<c;z++)
                {
                    scanf("%c",&dun[i][j][z]);
                    if(dun[i][j][z]==‘S‘)
                    {
                        sl=i,sr=j,sc=z;
                    }
                    if(dun[i][j][z]==‘E‘)
                    {
                        el=i,er=j,ec=z;
                    }
                }
                scanf("%*c");
            }
            scanf("%*c");
        }

    int ans=bfs();
    if(ans!=-1)
    {
        printf("Escaped in %d minute(s).\n",ans);
    }
    else
    {
        printf("Trapped!\n");
    }
}
    return 0;
}
时间: 2024-10-09 06:28:21

virtual judge(专题一 简单搜索 B)的相关文章

virtual judge(专题一 简单搜索 C)

Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

专题一 简单搜索

本专题主要锻炼搜索的两大方法——bfs (宽度优先搜索)和 dfs (深度优先搜索) ===================================华丽的分割线======================================= 一.bfs——宽度优先搜索 bfs主要运用于搜索中求最短时间的问题,搜索过程中一般需要运用 queue 的操作.具体的操作如下: 1.首先需要将 队列 和 visit数组 清空.(这一点很重要!!!) 2.然后将起点信息 push 进队列,标记为vis

bin巨专题一 简单搜索(更新中

A--棋盘问题 POJ-1321 链接: https://vjudge.net/problem/15202/origin 类似n皇后问题,需要注意的是dfs的边界问题(t在此处 思路:当前走到i/u行j列,判断该点是否可以放棋子,不可以就j++,可以就dfs(i + 1),对放的棋子数进行计数,若等于k则return,记得状态还原. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 us

[kuangbin带你飞]专题一 简单搜索

一直拖.放了放久.受不了 A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第

专题一 简单搜索 Problem A

题目链接:http://poj.org/problem?id=1321 题目大意和N皇后类似,只不过可能出现K<N的情况. 按照行的顺序进行深搜,如果当前行可以放置棋子,则换下一行进行搜索. 用一个二维数组描述棋盘,一个一维数组标记此列是否走过. 1 #include<cstdio> 2 #include<cstdlib> 3 4 const int MAXN=8; 5 char m[MAXN][MAXN]; //记录棋盘 6 int vis[MAXN]; //标记每一列是否

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master

B - Dungeon Master 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 c

[kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

1 //Memory Time 2 //2236K 32MS 3 4 #include<iostream> 5 using namespace std; 6 7 int mod[524286]; //保存每次mod n的余数 8 //由于198的余数序列是最长的 9 //经过反复二分验证,436905是能存储198余数序列的最少空间 10 //但POJ肯定又越界测试了...524286是AC的最低下限,不然铁定RE 11 12 int main(int i) 13 { 14 int n; 15

专题一 简单搜索 Problem B

题目链接:http://poj.org/problem?id=2251 题目大意:三维迷宫求两点间最短路. 解题思路:bfs搜索. 用一个三维数组maze记录迷宫的每一点是否可走. 用一个三维数组标记每一点是否已经走过. 用一个一维数组模拟队列的实现过程. 将起点放在队首,然后向六个方向扩展,若可行,则把该点放在队首,并抛弃队首成员. 如果当前点为终点,则记录下此时花费的时间,并返回. 代码如下: 1 #include<cstdio> 2 #include<cstring> 3 #