声明:图片及内容基于https://www.bilibili.com/video/BV1oE41177wk?t=3245

问题及分析

 

8*8的迷宫,最外周是墙,0表示可以走,1表示不可以走

设置迷宫

 

const int M = 8;  //迷宫长(不包含墙)
const int N = 8;  //迷宫宽(不包含墙)

 

设置方位坐标

class Box{  //栈中元素
public:
    int x, y;
    int di;       //方向及Direction数组的下标
    Box(int x, int y, int di):x(x),y(y),di(di) {}
    Box() {}
};

设置方位偏移量

class Direction{          //x,y方向增量
public:
    int incX, incY; 
    Direction(int x, int y) :incX(x), incY(y) {}
};

寻找路径

bool findPath(int maze[M + 2][N + 2], Direction direct[], stack<Box> &s) {
    Box temp(1,1,-1);  //temp用来记录当前所在位置
    int x, y, di;  //迷宫格子当前处理单元的纵横坐标和方向
    int line, col; //迷宫数组下一个单元的行坐标和列坐标
    maze[1][1] = -1;    //走过的格子设为-1
    s.push(temp);
    while (!s.empty()) {
        temp = s.top();     //每次获取栈的头元素
        s.pop();            //弹出头元素
        x = temp.x; y = temp.y; di = temp.di+1;  //上次循环到没法走要换个方向,更新当前位置,下,x,y不变,di+1,换个方向
        while (di < 4) {
            line = x + direct[di].incX;
            col = y + direct[di].incY;
            if (maze[line][col] == 0) {    //格子为0说明这个格子可以走
                Box temp2(x, y, di);
                temp = temp2;
                s.push(temp);           //成功走到下一个格子就压入栈
                x = line; y = col; maze[line][col] = -1; //更新当前所在位置
                if (x == M && y == N) return true;
                else di = 0;         //从最新的位置开始尝试四个方向
            }
            else di++;      //maze[line][col]== -1 没法走,换一个方向
        }
    }
    return false;       //没有路
}

完整代码

#include<iostream>
#include<stack>
using namespace std;
const int M = 8;  //迷宫长(不包含墙)
const int N = 8;  //迷宫宽(不包含墙)

class Direction{          //x,y方向增量
public:
    int incX, incY; 
    Direction(int x, int y) :incX(x), incY(y) {}
};


class Box{  //栈中元素
public:
    int x, y;
    int di;       //方向及Direction数组的下标
    Box(int x, int y, int di):x(x),y(y),di(di) {}
    Box() {}
};

bool findPath(int maze[M + 2][N + 2], Direction direct[], stack<Box> &s) {
    Box temp(1,1,-1);  //temp用来记录当前所在位置
    int x, y, di;  //迷宫格子当前处理单元的纵横坐标和方向
    int line, col; //迷宫数组下一个单元的行坐标和列坐标
    maze[1][1] = -1;    //走过的格子设为-1
    s.push(temp);
    while (!s.empty()) {
        temp = s.top();     //每次获取栈的头元素
        s.pop();            //弹出头元素
        x = temp.x; y = temp.y; di = temp.di+1;  
        while (di < 4) {
            line = x + direct[di].incX;
            col = y + direct[di].incY;
            if (maze[line][col] == 0) {    //格子为0说明这个格子可以走
                Box temp2(x, y, di);
                temp = temp2;
                s.push(temp);           //成功走到下一个格子就压入栈
                x = line; y = col; maze[line][col] = -1;
                if (x == M && y == N) return true;
                else di = 0;         //从最新的位置开始尝试四个方向
            }
            else di++;      //换一个方向
        }
    }
    return false;       //没有路
}

int main() {
    stack<Box> s;
    Direction direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
    int maze[M + 2][N + 2];
    for (int i = 0; i < M + 2; i++) {
        for (int j = 0; j < N + 2; j++) {
            cin >> maze[i][j];
        }
    }
    int maze2[M+2][N+2];              //用maze2拷贝maze用于最后打印
    for (int i = 0; i < M + 2; i++) {
        for (int j = 0; j < N + 2; j++) {
            maze2[i][j]= maze[i][j];
        }
    }
    if (findPath(maze, direct, s)) {
        while (!s.empty()) {
            cout << "(" << s.top().x << "," << s.top().y << ")" << endl;
            maze2[s.top().x][s.top().y] = s.top().di + 2;  //类似哈希散列,做特殊标记。墙是0~1,箭头是2~5
            s.pop();                                       //为了实现可视化
        }
        for (int i = 0; i < M + 2; i++) {        //路径可视化
            for (int j = 0; j < N + 2; j++) {
                switch (maze2[i][j]) {
                case 0:cout << "0 "; break;
                case 1:cout << "1 "; break;
                case 2:cout << ""; break;
                case 3:cout << ""; break;
                case 4:cout << ""; break;
                case 5:cout << ""; break;
                default:break;
                }
                //cout << maze2[i][j]<<" ";
            }
            cout << endl;
        }
    }
    else cout << "No path"<<endl;
    return 0;
}

输入:

1 1 1 1 1 1 1 1 1 1

1 0 0 1 0 0 0 1 0 1

1 0 0 1 0 0 0 1 0 1

1 0 0 0 0 1 1 0 0 1

1 0 1 1 1 0 0 0 0 1

1 0 0 0 1 0 0 0 0 1

1 0 1 0 0 0 1 0 0 1

1 0 1 1 1 0 1 1 0 1

1 1 0 0 0 0 0 0 0 1

1 1 1 1 1 1 1 1 1 1

输出:

 

(8,7)
(8,6)
(8,5)
(7,5)
(6,5)
(6,4)
(6,3)
(5,3)
(5,2)
(5,1)
(4,1)
(3,1)
(3,2)
(2,2)
(1,2)
(1,1)
1 1 1 1 1 1 1 1 1 1
1 →↓1 0 0 0 1 0 1
1 0 ↓1 0 0 0 1 0 1
1 ↓←0 0 1 1 0 0 1
1 ↓1 1 1 0 0 0 0 1
1 →→↓1 0 0 0 0 1
1 0 1 →→↓1 0 0 1
1 0 1 1 1 ↓1 1 0 1
1 1 0 0 0 →→→0 1
1 1 1 1 1 1 1 1 1 1

 

版权声明:本文为gonghr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gonghr/p/14515093.html