一直没弄明白的迷宫问题,今天竟然突然开窍啦。记录一下,好像也没有那么难。。。

本文介绍了一种使用栈和方向试探法解决迷宫问题的算法。通过定义迷宫地图,利用结构体存储位置信息,尝试向右、下、左、上四个方向前进,遇到障碍或边界则回溯。最终实现从起点到终点的路径寻找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
using namespace std;
#include <stack>

struct location
{
    int x;//行
    int y;//列
};

//定义一个地图,墙 = -1,路 = 0,走过的点 = 1, 已走过的回退的无效点 = 2
int MAP[10][11]={
       { 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
       { 0,  0,  0, -1, -1,  0,  0,  0,  0, -1, -1},
       {-1,  0, -1,  0, -1, -1,  0, -1,  0, -1, -1},
       {-1,  0, -1,  0, -1, -1,  0, -1,  0,  0,  0},
       {-1,  0, -1,  0,  0, -1,  0, -1, -1,  0, -1},
       {-1,  0, -1, -1,  0, -1,  0,  0, -1,  0, -1},
       {-1,  0, -1,  0,  0,  0, -1,  0,  0,  0,  0},
       {-1,  0, -1,  0, -1,  0, -1,  0, -1,  0, -1},
       {-1,  0, -0,  0, -1,  0,  0,  0, -1,  0,  0},
       {-1, -1, -1,  0, -1, -1, -1,  0, -1, -1,  0},
};

void print_map()
{
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 11; j++)
        {
            printf("%3d",MAP[i][j]);
        }
        printf("\n");
    }
}

//尝试下一个位置是否能走,顺序依次为,右、下、左、上
int isok(location current_loc)
{
    //如果下一个位置未被走过,即地图值为0,而且不会超过地图范围,则说明此位置可走,返回方向数字
    if(MAP[current_loc.x][current_loc.y+1] == 0 && current_loc.y+1 < 11)
    {
        return 1;
    }
    else if(MAP[current_loc.x+1][current_loc.y] == 0 && current_loc.x+1 < 10)
    {
        return 2;
    }
    else if(MAP[current_loc.x][current_loc.y-1] == 0 && current_loc.y-1 >= 0)
    {
        return 3;
    }
    else if(MAP[current_loc.x-1][current_loc.y] == 0 && current_loc.x-1 >=0)
    {
        return 4;
    }
    else
    {
        return 0;
    }
}
int main()
{
    stack<location> qs;
    int flag = -1;//标记是否已无路可走,初始值-1,0表示成功,1表示无解
    location start_loc;
    start_loc.x = 0;
    start_loc.y = 0;
    location end_loc;
    end_loc.x = 9;
    end_loc.y = 10;
    location current_loc;
    current_loc.x = start_loc.x;
    current_loc.y = start_loc.y;
    qs.push(current_loc);//将走过的结点压栈,以便与走到死胡同时的回溯
    while(1)
    {
        //将当前位置在地图上标记
        MAP[current_loc.x][current_loc.y] = 1;
        //如果当前位置和终点位置相同,则退出循环
        if(current_loc.x == end_loc.x && current_loc.y == end_loc.y)
        {
            flag == 0;
            break;
        }
        //尝试下一个位置
        int ret = isok(current_loc);
        switch (ret) {
        case 1:
            current_loc.y += 1;
            qs.push(current_loc);
            break;
        case 2:
            current_loc.x += 1;
            qs.push(current_loc);
            break;
        case 3:
            current_loc.y -= 1;
            qs.push(current_loc);
            break;
        case 4:
            current_loc.x -= 1;
            qs.push(current_loc);
            break;
        default:
        //若当前位置没有可走的下一步,且栈内没有可回溯的点,说明无法走到终点
            if(qs.empty())
            {
                flag = 1;
                break;
            }
            else
            {
                MAP[current_loc.x][current_loc.y] = 2;
                current_loc = qs.top();
                qs.pop();
            }
            break;
        }
    }
    if(flag == 0)
    {
        printf("success to solve the maze!\n");
        print_map();
    }
    else if(flag == 1)
    {
        printf("there is no way!\n");
        print_map();
    }
    else
    {
        printf("the application error!\n");
        print_map();
    }
    return 0;
}
前言: 最近学Windows shell外壳,偶然发现了SHChangeNotifyRegister这个神奇的函数,于是便用它写了个例程。 本帖不少思想来自帖子:未公开Windows API SHChangeNotifyRegister实现文件监控 基本介绍: 在Windows实现文件监控有三种方法,第一种是“虚拟文件系统驱动”方法,如windows 下的filemon,网上有很多关于他的分析。第二种方法是“HOOK API”方法,钩子技术。第三种方法是“消息机制”,从windows的文件通知消息获取系统的文件操作。但是这是文件操作完成以后,才通知的。所以只能进行监视监视,不能进行完全的控制。而消息机制当中,也有三种方法,(1)通过使用“未公开API SHChangeNotifyRegister 实现”;(2)通过 FindFirstChangeNotification 实现;(3)通过 ReadDirectoryChangesW 实现。第(2)(3)种方法只能针对一个在指定目录或子目录下发生的更改符合过滤条件时,进行监视。 而现在,易语言 中大部分消息机制监视文件使用FindFirstChangeNotification或ReadDirectoryChangesW + 线程实现的(例如:文件监控精灵 - 监控目录文件新建删除重命名修改 ),该方法效率较低,而且如果很多文件在短时间内发生变更,则有可能会丢失部分通知,且监视的文件信息有限,所以,我写了一份使用SHChangeNotifyRegister来监视文件的例程。 特性: 代码几乎是全注释,清晰明了: 程序很多命令和常量是翻译自MSDN,规范程度高: 监视多种消息(比如USB接口信息),很多问题一个命令即可解决:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值