基本信息
源码名称:c++ 迷宫构造 源码
源码大小:0.03M
文件格式:.zip
开发语言:C/C++
更新时间:2020-11-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍
用c/c 做一个控制台迷宫,可以自由行走,可以寻找最短路径
平台:vs2019

bool mgpath(int xi, int yi, int xe, int ye)//求解路径为:(xi,yi)->(xe,ye)
{
    int i, j;
    queue<point*> q;
    point* start = new point;//起点  
    start->x = xi;
    start->y = yi;
    start->last = start;
    q.push(start);
    mg[start->x][start->y] = 2;
    point end;//终点  
    end.x = xe;
    end.y = ye;
    int aspect[4][2] = { { 0, -1 },{ 0, 1 },{ -1, 0 },{ 1, 0 } };//转向
    int flag = 0;//是否有路可走的标志  
    while (!q.empty()) {
        point* go = q.front();
        q.pop();
        if (go->x == xe && go->y == ye) {
            flag = 1;
            mg[go->x][go->y] = -6;
            point* lastPoint = go;
            go = go->last;
            while ((go->x != start->x) || (go->y != start->y)) {
                if (lastPoint->x - go->x == 1) {
                    mg[go->x][go->y] = -1;
                }
                else if (lastPoint->x - go->x == -1) {
                    mg[go->x][go->y] = -2;
                }
                else if (lastPoint->y - go->y == 1) {
                    mg[go->x][go->y] = -3;
                }
                else {
                    mg[go->x][go->y] = -4;
                }
                lastPoint = go;
                go = go->last;
            }
            mg[start->x][start->y] = -5;
            break;
        }
        else {
            for (int i = 0; i < 4; i) {
                point* temp = new point;
                temp->x = go->x aspect[i][0];
                temp->y = go->y aspect[i][1];
                if (mg[temp->x][temp->y] == 0) {
                    temp->last = go;
                    q.push(temp);
                    mg[temp->x][temp->y] = mg[go->x][go->y] 1;
                }
            }
        }
    }
    if (!flag)
    {
        Z = 1;
        return false;
    }
    else {
        Z = 0;
        return true;
    }
}