Steve_Abelieve

Don't worry too much about the ambiguous future, just make effort for explicit being present.

迷宫出口以及迷宫最短路径的求解

整理出来方便大家复习

//完成迷宫
#pragma warning (disable:4996)
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
#include<assert.h>


//创建迷宫->动态创建二维数组:1.int[][N]  2.int*->(二维数组是按照一维数组来存储的)
//打印迷宫
//走迷宫

const size_t N = 10;//定义全局变量N

struct Pos
{
    int _row;
    int _col;
};

//思考:三元组->适合适合什么问题?迷宫?二维数组?
void InitMaze(int maze[][N])
{
    FILE*fout = fopen("MazeMap.txt", "r");
    assert(fout);

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N;)
        {
            //获取文件中的字符用fgetc()

            char ch = fgetc(fout);
            if (ch == '0' || ch == '1')//因为在文件中保存,所有数字均为字符数字
            {
                maze[i][j] = ch-'0';
                ++j;
            }

        }

    }
}


void PrintMaze(int maze[][N])
{
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
        {

            cout << maze[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

}


bool CheckAccess(int maze[][N], Pos tmp)
{
    if (tmp._row >= 0 && tmp._row < N
        &&tmp._col >= 0 && tmp._col < N
        &&maze[tmp._row][tmp._col] == 0)
    {
        return true;
    }

    return false;

}

//获取迷宫路径
void GetMazePath(int maze[][N], Pos entry)
{

    //回溯法->将路径压入栈
    //试探法,对每个方向进行试探一下

    stack<Pos>path;
    path.push(entry);

    while (!path.empty())
    {
        Pos cur = path.top();
        Pos next = cur;

        if (cur._row == N - 1)
        {
            break;
        }

        //试探法,对每个方向进行试探一下
        //上方  
        next._row--;
        if (CheckAccess(maze,next))
        {
            maze[next._row][next._col] = 2;
            path.push(next);
            continue;
        }

        next = cur;
        //右方
        next._col++;
        if (CheckAccess(maze, next))
        {
            maze[next._row][next._col] = 2;
            path.push(next);
            continue;
        }

        next = cur;

        //下方  
        next._row++;
        if (CheckAccess(maze, next))
        {
            maze[next._row][next._col] = 2;
            path.push(next);
            continue;
        }

        next = cur;

        //右方
        next._col--;
        if (CheckAccess(maze, next))
        {
            maze[next._row][next._col] = 2;
            path.push(next);
            continue;
        }

        //四个方向都走不通-》回溯法

        maze[cur._row][cur._col] = 3;

        path.pop();
    }

}



//求迷宫最优解:-》 


//考虑两个问题:1.怎么标记的,还走回去?
//2.递归怎么解决子问题
//思路:肯定要把每个出口进行遍历,比较出一条最短的路径来

bool CheckAccess(int maze[][N],size_t n, Pos next,Pos cur)
{
    if (next._row >= 0 && next._row < n
        &&next._col >= 0 && next._col < n)
    {
        if (maze[next._row][next._col] == 0 || maze[next._row][next._col]>maze[cur._row][cur._col])
        {
            return true;
        }
    }

    return false;

}


bool GetMazePathR(int maze[][N], size_t n,Pos cur,stack<Pos>&shortPath,stack<Pos>&path)
{
    path.push(cur);

    if (cur._row == n - 1)
    {
        if (shortPath.empty() || path.size() < shortPath.size())
        {
            shortPath = path;
        }

        //return true;//一旦找到路径就结束
    }

    Pos next = cur;

    //四个方向进行检测
    next._row++;

    if (CheckAccess(maze,n,next,cur))
    {
        maze[next._row][next._col] = maze[cur._row][cur._col] + 1;

        if ( GetMazePathR(maze, n, next, shortPath, path))
        {
            return true;

        }

    }

    next = cur;
    next._col++;

    if (CheckAccess(maze, n,next,cur ))
    {
        maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
        if (GetMazePathR(maze, n, next, shortPath, path))
        {
            return true;
        }

    }

    next = cur;
    next._row++;

    if (CheckAccess(maze, n,next,cur ))
    {
    maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
    if (GetMazePathR(maze, n, next, shortPath, path))
    {
        return true;

    }

    }

    next = cur;
    next._row--;
    if (CheckAccess(maze, n,next,cur ))
    {
    maze[next._row][next._col] = maze[cur._row][cur._col] + 1;
    if (GetMazePathR(maze, n, next, shortPath, path))
    {
        return true;

    }

    }
    path.pop();//退到上一个路径上
    return false;

}




int main()
{
    int maze[N][N];
    InitMaze(maze);
    PrintMaze(maze);

    Pos entry = { 1,0 };
    //GetMazePath(maze, entry);
    //PrintMaze(maze);


    stack<Pos>path;
    stack<Pos>shortPath;
    GetMazePathR(maze,N,entry,shortPath,path );
    PrintMaze(maze);


    return 0;
}

迷宫通路:
这里写图片描述
迷宫最优解:
这里写图片描述

阅读更多
版权声明:惜不吝赐教,欢迎指点。 https://blog.csdn.net/lizhidefengzi/article/details/70261512
个人分类: 数据结构
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

迷宫出口以及迷宫最短路径的求解

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭