栈的应用———迷宫问题

来源:互联网 发布:匡威帆布鞋淘宝店代理 编辑:程序博客网 时间:2024/06/06 03:20

一、简单的有一条活路的迷宫

算法描述:
这里写图片描述

#include <iostream>#include <stack>using namespace std;struct Seat     // 坐标位置{    Seat(int x, int y)    :_x(x)    , _y(y)    {}    int _x;    int _y;};#define ROW 10#define COL 10class Masze{public:    Masze(int map[][COL])    {        for (int i = 0; i < ROW; ++i)        {            for (int j = 0; j < COL; ++j)            {                _map[i][j] = map[i][j];            }        }    }    bool IsPass(const Seat& s)      //判断下一步是否能走    {        if ((_map[s._x][s._y] == 1)|| IsExit(s))            return true;        return false;    }    bool IsExit(const Seat& s)   // 判断是否为出口    {        if (s._x < 0 || s._x >= ROW || s._y < 0 || s._y >= COL)        {            return true;        }        return false;    }    bool PassMaze(Seat& enter)     // 迷宫是否能走通    {            if (_map[enter._x][enter._y] != 1)        {            return false;        }        stack<Seat> s;        s.push(enter);        while (!s.empty())        {            Seat Cur = s.top();            if (IsExit(Cur))            {                return true;            }            _map[Cur._x][Cur._y] = 2;            Seat up(Cur._x-1, Cur._y);            if (IsPass(up))            {                s.push(up);                continue;            }            Seat left(Cur._x , Cur._y - 1);            if (IsPass(left))            {                s.push(left);                continue;            }            Seat right(Cur._x, Cur._y + 1);            if (IsPass(right))            {                s.push(right);                continue;            }            Seat down(Cur._x + 1, Cur._y);            if (IsPass(down))            {                s.push(down);                continue;            }            _map[Cur._x][Cur._y] = 3;            s.pop();        }        return false;    }    void PrintMap()     {            for (int i = 0; i < ROW; ++i)            {                for (int j = 0; j < COL; ++j)                {                    cout << _map[i][j] << "   ";                }                cout << endl;            }            cout << endl;    }private:    int _map[ROW][COL];};int main(){    int arr[ROW][COL] = {        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },        { 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }    };    Masze m(arr);    Seat enter(9, 5);    m.PassMaze(enter);    m.PrintMap();    return 0;}

运行之:

这里写图片描述
二。多条通路最短路问题

这里写图片描述

#include <stack>#include <iomanip>#include <vector>using namespace std;struct Seat             // 位置坐标{    Seat(int x, int y)    :_x(x)    , _y(y)    {}    bool operator==(const Seat& s)    {        return (_x == s._x) && (_y == s._y);    }    bool operator!=(const Seat& s)    {        return !(*this == s);    }    int _x;    int _y;};class Masze{public:    Masze(int* map, int row, int col)     //利用vector创建二维数组        :_row(row)        , _col(col)    {        _map.resize(row);                       // 给出行               /*_map = new int*[row];*/        for (int i = 0; i < row; ++i)          {            _map[i].resize(col);            //每一行的的列            /*_map[i] = new int[col];*/            for (int j = 0; j <_col; ++j)            {                _map[i][j] = map[col*i + j];            }        }    }    bool IsPass(const Seat& cur, const Seat& next)     //判断下一步可不可以走    {        /*if (IsExit(cur))            return false;*/        if (_map[next._x][next._y] == 0)       // 0 为死路            return 0;        if ((_map[next._x][next._y] == 1))     // 1 为活路            return true;        else if ((_map[cur._x][cur._y] < _map[next._x][next._y]) && (_map[cur._x][cur._y] + 1 != _map[next._x][next._y]))            return true;                              // 当前步小于下一步可以走与不是走过的路        return false;    }    bool IsExit(const Seat& s)    {        if (s._x == 0 || s._x == _row - 1 || s._y == 0 || s._y  == _col - 1)    // 判断是不是出口        {            return true;        }        return false;    }    bool PassMaze(Seat& enter)          // 迷宫是否能走出去    {        if (_map[enter._x][enter._y] != 1)   //检测入口        {            return false;        }        stack<Seat> s;           // 保存走过每一步        stack<Seat> shortpath;   //保存最短路        s.push(enter);                  bool flag = false;        while (!s.empty())      // 最后从入口点退出        {            Seat Cur = s.top();               if (flag  && IsExit(Cur))                     //    第一步不是出口 && 判断出口            {                   if (Cur != enter)                            //  最后为入口点  出口不等于入口                 if (shortpath.empty() || (s.size() < shortpath.size()))   // 将最短路放在shortpatn里                {                    shortpath = s;                }                if (!s.empty())                s.pop();                                               //  到出口退栈                continue;            }            if (!flag && Cur == enter)  // 先走第一步  但不是最后一步            {                _map[Cur._x][Cur._y] = _map[Cur._x][Cur._y] + 1; //下一步为当前步加1                flag = true;            }            Seat up(Cur);            up._x -= 1;            if (IsPass(Cur ,up))                    // 向上走            {                _map[up._x][up._y] = _map[Cur._x][Cur._y] + 1;                s.push(up);                continue;            }            Seat left(Cur._x, Cur._y - 1);  // 向左走            if (IsPass(Cur, left))            {                _map[left._x][left._y] = _map[Cur._x][Cur._y] + 1;                s.push(left);                continue;            }            Seat right(Cur._x, Cur._y + 1);  //向右走            if (IsPass(Cur, right))            {                _map[right._x][right._y] = _map[Cur._x][Cur._y] + 1;                s.push(right);                continue;            }            Seat down(Cur._x + 1, Cur._y);  //向下走            if (IsPass(Cur ,down))            {                _map[down._x][down._y] = _map[Cur._x][Cur._y] + 1;                s.push(down);                continue;            }            s.pop();  // 上下左右都不能走退栈        }        if (!shortpath.empty())            return true;        return false;    }    void PrintMap()    {        for (int i = 0; i < _row; ++i)        {            for (int j = 0; j < _col; ++j)            {                cout << setw(5)<<left<<_map[i][j] ;            }            cout << endl;        }        cout << endl;    }private:    vector<vector<int>>  _map;    int _row;    int _col;};int main(){    int arr[10][10] = {        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },        { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },        { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },        { 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },        { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }    };    Masze m((int*)arr, 10, 10);    Seat enter(9, 5);    cout << m.PassMaze(enter) << endl;    m.PrintMap();    return 0;}

运行之:

原创粉丝点击