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\n");
        }
        else{
            printf("NO\n");
        }
    }

    //fclose(stdin);
    return 0;
}
复制代码

 

posted @   Lucky小黄人^_^  阅读(206)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示