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;
    }

  • 相关阅读:
    android四大组件--ContentProvider具体解释
    C语言指针的初始化和赋值
    微信游戏《全民炫舞》公司的引擎开发和布料系统技术介绍
    Struts2自己定义拦截器实例—登陆权限验证
    数据仓库与数据挖掘的一些基本概念
    acm省赛选拔组队赛经验谈
    【我所认知的BIOS】—&gt; uEFI AHCI Driver(5) — 第一个protocol最终要開始安装了
    ios项目开发(天气预报项目):使用正则获取 weather.com.cn站点信息
    每日总结-05-16(再见强哥有感)
    编程算法基础-2.7作业-通讯编码-格式检查
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5649850.html
Copyright © 2011-2022 走看看