迷宫问题

利用DFS,可以是递归或非递归的。

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define WIDTH 5
/*
    0: 不可以走
    1: 可以走
    -1: 已经走过了
    -2:路径不可行
    2: 最终路径
*/
int maze[WIDTH][WIDTH] = {      {0,0,0,0,0},
                                {0,1,0,1,0},
                                {0,1,0,1,0},
                                {0,1,1,1,0},
                                {0,0,0,0,0}};
typedef struct {
    pair<int,int> seat;
    int di; // 0 1 2 3
} SElemType;

void printMaze(){
    for(int i = 0;i < WIDTH; ++i){
        for(int j = 0; j < WIDTH; ++j){
            cout << maze[i][j] << "\t";
        }
        cout << endl;
    }
}
pair<int,int> nextPos(pair<int,int> curPos, int dir){
    switch(dir){
    case 0:
        return make_pair(curPos.first, curPos.second-1); // left
    case 1:
        return make_pair(curPos.first+1, curPos.second); // down
    case 2:
        return make_pair(curPos.first, curPos.second+1); // right
    case 3:
        return make_pair(curPos.first-1, curPos.second); // up
    }
}
bool mazePath(pair<int, int> startPos, pair<int, int> endPos){
    stack<SElemType> pathStack;
    pair<int,int> curPos = startPos;
    do{
        if(maze[curPos.first][curPos.second] == 1){ // first time
            maze[curPos.first][curPos.second] = -1; // 走过
            SElemType e;
            e.seat  = curPos;
            e.di    = 0;
            pathStack.push(e);
            if(curPos == endPos){
                cout << "success" << endl;
                // 此时pathStack存储路径。
                while(!pathStack.empty()){
                    e = pathStack.top();
                    pathStack.pop();
                    maze[e.seat.first][e.seat.second] = 2;
                }
                return true;
            }
            curPos = nextPos(curPos, 0);
        }
        else{
            if(!pathStack.empty()){
                SElemType e = pathStack.top();
                pathStack.pop();
                while(e.di == 3 && !pathStack.empty()){
                    // 路径不可行
                    maze[curPos.first][curPos.second] = -2;
                    e = pathStack.top();
                    pathStack.pop();
                }
                if(e.di < 3){
                    e.di++;
                    pathStack.push(e);
                    curPos = nextPos(e.seat, e.di);
                }
            }
        }
    }while(!pathStack.empty());
    cout << "failure" << endl;
    return false;
}
int main(){
    printMaze();
    pair<int, int> startPos(1,1);
    pair<int, int> endPos(1,2);
    mazePath(startPos,endPos);
    printMaze();
    return 0;
}

PS:

深度优先搜索与广度优先搜索算法有何区别呢?
  通常深度优先搜索法不全部保留结点,扩展完的结点从数据库中弹出删去,这样,一般在数据库中存储的结点数就是深度值,因此它占用空间较少。所以,当搜索树的结点较多,用其它方法易产生内存溢出时,深度优先搜索不失为一种有效的求解方法。
  广度优先搜索算法,一般需存储产生的所有结点,占用的存储空间要比深度优先搜索大得多,因此,程序设计中,必须考虑溢出和节省内存空间的问题。但广度优先搜索法一般无回溯操作,即入栈和出栈的操作,所以运行速度比深度优先搜索要快些

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/zhouweiabc/article/details/39202979
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞