用栈实现迷宫求解问题

        用栈实现迷宫求解问题。

输入:

        迷宫的行、列

        迷宫数组(1为墙)

输出:

        迷宫解的路径坐标

#include <iostream>
using namespace std;

class NODE {
    int x, y;
  public:
    int lastx, lasty; //上一处的位置

    NODE(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
    friend class SHED;
    friend bool waysearch(int w, int h);
};
class SHED {
  public:
    NODE* a;
    int maxsize;//迷宫大小
    int top;//栈顶指针
    int n;//栈元素个数
    SHED(int maxsize) {
        this->n = 0;
        this->maxsize = maxsize;
        this->a = new NODE[maxsize];
        this->top = -1;
    }
    SHED() {
        n = 0;
        maxsize = 0;
        a = nullptr;
        top = -1;
    }
    bool iffull() {
        if (n < maxsize)return true;
        return false;
    }
    bool ifempty() {
        if (n == 0)return true;
        return false;
    }
    void push(NODE b) {
        a[top + 1].x = b.x;
        a[top + 1].y = b.y;
        a[top + 1].lastx = b.lastx;
        a[top + 1].lasty = b.lasty;
        n++;
        top++;
    }
    void push(int x, int y) {
        a[top + 1].x = x;
        a[top + 1].y = y;
        n++;
        top++;
    }
    void pop() {
        a[top].x = -1;
        a[top].y = -1;
        top--;
        n--;
    }
    void change() {
        for (int i = 0; i < n / 2; i++) {
            int p = a[i].x;
            int q = a[i].y;
            a[i].x = a[n - 1 - i].x;
            a[n - 1 - i].x = p;
            a[i].y = a[n - 1 - i].y;
            a[n - 1 - i].y = q;
        }
    }
    void print() {
        change();
        for (int i = 0; i < n; i++) {
            cout << a[i].y << "," << a[i].x << endl;
        }
    }
};
class CELL {
  public:
    bool ifwall, ifpass;
  public:
    CELL() {
        ifwall = false;
        ifpass = false;
    }
};
bool ifcouldpass(CELL a) {
    if (a.ifwall == false && a.ifpass == false) {
        return true;
    }
    return false;
}
bool waysearch(int w, int h) { //宽度与高度
    bool ifsearch = false;
    CELL maze[h][w];
    SHED shed(w * h);
    NODE in, out; //入口出口
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int num;
            cin >> num;
            if (num == 1) {
                maze[i][j].ifwall = true;
            } else {
                maze[i][j].ifwall = false;
            }
            if (num == 3) {
                in.x = i;
                in.y = j;
            }
            if (num == 4) {
                out.x = i;
                out.y = j;
            }
            maze[i][j].ifpass = false;
        }
    }
    shed.push(in);
    maze[in.x][in.y].ifpass = true;
    while (shed.n < shed.maxsize) {
        int x0 = shed.a[shed.top].x;
        int y0 = shed.a[shed.top].y;
        if (maze[x0 + 1][y0].ifwall == false &&
                maze[x0 + 1][y0].ifpass == false) { //下面
            NODE p(x0 + 1, y0);
            p.lastx = x0;
            p.lasty = y0;
            shed.push(p);
            maze[p.x][p.y].ifpass = true;
            if (p.x == out.x && p.y == out.y) {
                ifsearch = true;
                break;
            }
        }
        if (maze[x0][y0 - 1].ifwall == false &&
                maze[x0][y0 - 1].ifpass == false) { //左面
            NODE p(x0, y0 - 1);
            p.lastx = x0;
            p.lasty = y0;
            shed.push(p);
            maze[p.x][p.y].ifpass = true;
            if (p.x == out.x && p.y == out.y) {
                ifsearch = true;
                break;
            }
        }
        if (maze[x0 - 1][y0].ifwall == false &&
                maze[x0 - 1][y0].ifpass == false) { //上面
            NODE p(x0 - 1, y0);
            p.lastx = x0;
            p.lasty = y0;
            shed.push(p);
            maze[p.x][p.y].ifpass = true;
            if (p.x == out.x && p.y == out.y) {
                ifsearch = true;
                break;
            }
        }
        if (maze[x0][y0 + 1].ifwall == false &&
                maze[x0][y0 + 1].ifpass == false) { //右面
            NODE p(x0, y0 + 1);
            p.lastx = x0;
            p.lasty = y0;
            shed.push(p);
            maze[p.x][p.y].ifpass = true;
            if (p.x == out.x && p.y == out.y) {
                ifsearch = true;
                break;
            }
        }
        if (ifcouldpass(maze[shed.a[shed.top].x - 1][shed.a[shed.top].y]) == false &&
                ifcouldpass(maze[shed.a[shed.top].x][shed.a[shed.top].y + 1]) == false &&
                ifcouldpass(maze[shed.a[shed.top].x + 1][shed.a[shed.top].y]) == false &&
                ifcouldpass(maze[shed.a[shed.top].x][shed.a[shed.top].y - 1]) == false) {
            shed.pop();
        }
    }
    SHED way(shed.n);
    way.push(shed.a[shed.top]);
    NODE m(shed.a[shed.top].x, shed.a[shed.top].y);
    m.lastx = shed.a[shed.top].lastx;
    m.lasty = shed.a[shed.top].lasty;
    for (int i = shed.top - 1; i >= 0; i--) {
        if (shed.a[i].x == m.lastx && shed.a[i].y == m.lasty) {
            way.push(shed.a[i]);
            m.x = shed.a[i].x;
            m.y = shed.a[i].y;
            m.lastx = shed.a[i].lastx;
            m.lasty = shed.a[i].lasty;
        }
    }
    way.print();

    return ifsearch;
}
int main() {
    int w, h;
    cin >> w >> h;
    waysearch(w, h);
}

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值