zoukankan      html  css  js  c++  java
  • POJ2551Dungeon Master

    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!
    
    这道题是一道3D的搜索,可以用BFS,因为我最近正在练习BFS所以写一下,本来会的好多东西因为期末考试复习两周忘了好多,不过更好,正好从新学习,为蓝桥被准备!
    不再墨迹了;
    这题其实只是一道普通的搜索,不过我WA了几回后用自己的测试数据检验发现队列总是清不空,这一我选用的是用全局变量每次都重新定义队列,这样果然AC了;
    所以我们有时候可以用队列时都重新定义,及把它放在BFS里;我觉得这是一个小技巧,所以发一下;代码如下:

    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    int dirx[6]={1,-1,0,0,0,0};
    int diry[6]={0,0,1,-1,0,0};
    int dirz[6]={0,0,0,0,1,-1};
    char maps[31][31][31];
    int vis[31][31][31];
    int L,R,C;
    struct point
    {
    int x;
    int y;
    int z;
    int step;
    };
    point start,exit;
    int bfs()
    {
    queue<point>que;
    que.push(start);
    point temp,cur;
    memset(vis,-1,sizeof(vis));
    vis[start.x][start.y][start.z]=0;

    while(!que.empty())
    {
    temp=que.front();
    // cout<<temp.step<<"HH"<<endl;
    que.pop();
    if(maps[temp.x][temp.y][temp.z]=='E')
    {
    return vis[temp.x][temp.y][temp.z];
    break;
    }

    for(int i=0;i<6;i++)
    {
    cur.x=temp.x+dirx[i];
    cur.y=temp.y+diry[i];
    cur.z=temp.z+dirz[i];
    if(cur.x>0&&cur.x<=L&&cur.y>0&&cur.y<=R&&cur.z>=0&&cur.z<=C&&maps[cur.x][cur.y][cur.z]!='#'&&vis[cur.x][cur.y][cur.z]==-1)
    {
    cur.step=temp.step+1;
    que.push(cur);
    vis[cur.x][cur.y][cur.z]=cur.step;
    // cout<<cur.step<<endl;
    }
    }
    }
    return -1;
    }
    int main()
    {


    while(cin>>L>>R>>C)
    {

    if(L==0&&R==0&&C==0)
    {
    break;
    }


    for(int i=1;i<=L;i++)
    {
    for(int j=1;j<=R;j++)
    {
    for(int k=1;k<=C;k++)
    {
    cin>>maps[i][j][k];
    if(maps[i][j][k]=='S')
    {
    start.x=i;
    start.y=j;
    start.z=k;
    start.step=0;
    }
    }

    }
    }

    // cout<<start.step<<"mm"<<endl;
    int a=bfs();
    if(a==-1)
    {
    cout<<"Trapped!"<<endl;
    }
    else
    printf("Escaped in %d minute(s). ",a);


    }
    }

  • 相关阅读:
    @JsonFormat和@DateTimeFormat
    13位时间戳和时间格式化转换,工具类
    springboot配置hibernate jpa多数据源
    Mysql向数据库插入数据时,判断是否存在,若不存在就插入数据
    服务器启动完成执行定时任务Timer,TimerTask
    java中服务器启动执行定时任务
    Java定时任务
    阿里大鱼短信发送,放到项目中报错Java.lang.NoClassDefFoundError:com/aliyuncs/exceptions/ClientException,已解决
    MD5加密(相同的字符串,每次加密后的密文是相同的)
    常见的集中加密方法BASE64、MD5、SHA、HMAC
  • 原文地址:https://www.cnblogs.com/VectorLin/p/5127012.html
Copyright © 2011-2022 走看看