zoukankan      html  css  js  c++  java
  • dfs题型二(迷宫问题)

    取自:《王道论坛计算机考研机试指南》6.5节

    例 6.7 Temple of the bone(九度 OJ 1461)
    时间限制:1 秒 内存限制:32 兆 特殊判题:否
    题目描述:
    The doggie found a bone in an ancient maze, which fascinated him a lot.
    However, when he picked it up, the maze began to shake, and the doggie could feel
    the ground sinking. He realized that the bone was a trap, and he tried desperately to
    get out of this maze.
    The maze was a rectangle with sizes N by M. There was a door in the maze. At
    the beginning, the door was closed and it would open at the T-th second for a short
    period of time (less than 1 second). Therefore the doggie had to arrive at the door on
    exactly the T-th second. In every second, he could move one block to one of the upper,
    lower, left and right neighboring blocks. Once he entered a block, the ground of this
    block would start to sink and disappear in the next second. He could not stay at one
    block for more than one second, nor could he move into a visited block. Can the poor
    doggie survive? Please help him.
    输入:
    The input consists of multiple test cases. The first line of each test case contains
    three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the
    maze and the time at which the door will open, respectively. The next N lines give the
    maze layout, with each line containing M characters. A character is one of the
    following:
    'X': a block of wall, which the doggie cannot enter;
    'S': the start point of the doggie;
    'D': the Door; or
    '.': an empty block.
    The input is terminated with three 0's. This test case is not to be processed.
    输出:
    For each test case, print in one line "YES" if the doggie can survive, or "NO"
    otherwise.
    样例输入:
    4 4 5
    S.X.
    ..X.
    ..XD
    ....
    3 4 5
    S.X.
    ..X.
    ...D
    0 0 0
    样例输出:
    NO
    YES

    代码:

    #include <stdio.h>
    #include <cstring>
    const int maxn = 9;
    char maze[maxn][maxn];            //迷宫数组
    int status[maxn][maxn] = { 0 };
    int n, m, t;
    bool flag = false;                    //是否有解的标记
    
    //四个方向
    int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
    
    void dfs(int row, int col, int nowK){
        //边界条件
        if (maze[row][col] == 'D' && nowK == t){
            flag = true;
            return;
        }
    
        if (nowK > t) return;
    
        //把这个位置标记为已访问
        status[row][col] = 1;
    
        int newRow, newCol;
        for (int i = 0; i < 4; i++){
            newRow = row + dir[i][0];
            newCol = col + dir[i][1];
            if (newRow < n && newRow >= 0 && newCol < m && newCol >= 0 && maze[newRow][newCol] != 'X' && status[newRow][newCol] == 0)
                dfs(newRow, newCol, nowK + 1);
        }
    
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        //读取maze数组,读取的过程中获取S的位置
        while (true){
            scanf("%d %d %d", &n, &m, &t);
            if (0 == n && 0 == m && 0 == t){
                break;
            }
    
            int row, col;                //记录doggie的初始位置
    
            //重新初始化status数组
            memset(status, 0, sizeof(status));
            flag = false;
            getchar();        //读取回车
    
            for (int i = 0; i < n; i++){
                for (int j = 0; j < m; j++){
                    scanf("%c", &maze[i][j]);
                    if ('S' == maze[i][j]){
                        row = i;
                        col = j;
                    }
    
                }
                getchar();        //读取回车
            }
    
            //dfs
            dfs(row, col, 0);
    
            //输出
            if (flag){
                printf("YES
    ");
            }
            else{
                printf("NO
    ");
            }
        }
    
        //fclose(stdin);
        return 0;
    }
  • 相关阅读:
    ##日常代码中,我们需要配的几种数据源的配置文件
    ##Sping框架IOC中常用的注解大家一定要记住
    ##事务管理器工具类
    ##管理连接的工具类 用来实现连接 和线程的绑定
    ##处理转账事务的多线程问题,让其在一个线程处理账务的增加和减少
    学习笔记——spark安装配置
    学习笔记——spark基础实验二
    学习笔记——spark实验一
    课堂测试之数据清洗
    Mapreduce实例——WordCount
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/11463346.html
Copyright © 2011-2022 走看看