zoukankan      html  css  js  c++  java
  • poj2251 三维简单BFS

    D - (热身)简单宽搜回顾
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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!
    解析见代码:

    /*
    三维BFS,寻找最短路
    注意事项:首先是初始化,每输入一组数据记得要先将队列清空
    另外要注意因为字符输入比较多,还多有空行,一定要把必要的回车符吃掉
    三维的BFS和二维的并没有多少区,就是标记数组和地图数组开成三维的就可以
    状态的转移,从一个点因为是三维的,本来应该是有6个移动方向,但是因为出口肯定是在
    上面,所以没有必要往下走
    */
    #include <iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    bool visit[35][35][35];
    char map[35][35][35];
    int flag;
    int l,r,c;
    struct node
    {
    int x,y,z;
    int s;
    };
    queue<node> q;
    void init()
    {
    while(!q.empty())
    q.pop();
    memset(visit,false,sizeof(visit));
    memset(map,'.',sizeof(map));
    flag=0;
    }
    int main()
    {
    int i,j,k;
    char ch;
    while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))
    {
    init();
    getchar();//吃回车
    node start;
    for(i=0;i<l;i++)
    { for(j=0;j<r;j++)
    { for(k=0;k<c;k++)//三维的输入
    {
    cin>>ch;
    map[i][j][k]=ch;
    if(ch=='S') //记录起点位置
    {
    start.x=i;
    start.y=j;
    start.z=k;
    start.s=0;
    }
    }
    getchar();
    }
    getchar();
    }
    q.push(start);
    visit[start.x][start.y][start.z]=1;//标记
    while(!q.empty())
    {
    node m=q.front();
    q.pop();
    if(map[m.x][m.y][m.z]=='E')
    {
    printf("Escaped in %d minute(s). ",m.s);
    flag=1;//已经找到
    break;
    }
    m.s++;
    node m2;
    if(m.x-1>=0)
    {
    m2=m;
    m2.x--;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    if(m.x+1<l)
    {
    m2=m;
    m2.x++;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    if(m.y-1>=0)
    {
    m2=m;
    m2.y--;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    if(m.y+1<r)
    {
    m2=m;
    m2.y++;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    if(m.z-1>=0)
    {
    m2=m;
    m2.z--;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    if(m.z+1<c)//向上走,不会向下走
    {
    m2=m;
    m2.z++;
    if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
    { visit[m2.x][m2.y][m2.z]=1;
    q.push(m2);
    }
    }
    }
    if(!flag)
    puts("Trapped!");
    }
    return 0;
    }

  • 相关阅读:
    合并ts文件
    Typora
    Typora
    OCMock 3 参考
    git 修改上次提交信息 与 撤销此操作.
    git使用技巧
    python获取软件安装列表2222
    【Dojo 1.x】笔记6 配置对象dojoConfig的用处和真身所在
    【Dojo 1.x】笔记目录
    【Dojo 1.x】笔记5 使用本地引用
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5649850.html
Copyright © 2011-2022 走看看