UVa 784 - Maze Exploration

77 篇文章 0 订阅
19 篇文章 0 订阅

传送门UVa 784 - Maze Exploration


挺简单的题目.

给一个迷宫, 用'#'输出所有能走的地方.

只要用DFS就可以了, 搜索四个方向.

其实我很好奇他给出墙壁的宽是3point的目的是什么...


#include <cstdio>
#include <cstring>
using namespace std;

char maze[40][100];

char ch;
void DFS(int x, int y);

int main()
{
    //freopen("input.txt", "r", stdin);
    int i, T, k;
    int x, y;
    bool isFind;
    scanf("%d%*c", &T);
    while (T--)
    {
        k = 0;
        isFind = false;
        while (gets(maze[k++]))
        {
            if (maze[k - 1][0] == '_')
                break;
            if (isFind == false)
                for (i = 0; i < strlen(maze[k - 1]); i++)       //记录*的坐标.
                    if (maze[k - 1][i] == '*')
                    {
                        x = k - 1;
                        y = i;
                        isFind = true;
                    }

        }
        ch = maze[0][0];
        //接下来开始遍历.
        DFS(x, y);
        for (i = 0; i < k; i++)
            puts(maze[i]);
        memset(maze, 0, sizeof(maze));
    }
    return 0;
}

void DFS(int x, int y)
{
    if (maze[x][y] == ch || maze[x][y] == '#')
        return;
    maze[x][y] = '#';
    DFS(x - 1, y);
    DFS(x, y + 1);
    DFS(x, y - 1);
    DFS(x + 1, y);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值