数据结构——迷宫问题(顺序栈、C++)

 讲解:

一、采用二维数组和srand函数随机生成只有0和1的迷宫。

二、求解迷宫大概思路:先将入口处的坐标即方向d入栈,然后当栈不为空时,取出栈顶(即当前节点)的数据。遍历当前节点的四个方向,找到可行的下一个节点,并将其入栈;如没有可行的下一个节点,则将当前节点值0(表示未走过),然后出栈,退回到前一个节点进行遍历。当栈顶数据和出口一致时,输出迷宫的通路。

#include <iostream>
#include <time.h>
#define M 100
#define N 100
#define MAXSIZE 100
using namespace std;

int maze[M][N];

void CreateMaze(int m, int n)       //创建迷宫
{
    srand(time(NULL));
    for(int i=1;i<m+1;i++)
    {
        for(int j=1;j<n+1;j++)
        {
           maze[i][j] = rand()%2;    //只有0和1的随机迷宫
        }
    }

    maze[1][1] = -1; //直接入口置-1,表示可走
    maze[m][n] = 0; //出口

    for(int i=0;i<m+2;i++) maze[i][0] = 1;    //设置围墙
    for(int i=0;i<m+2;i++) maze[i][m+1] = 1;
    for(int j=0;j<n+2;j++) maze[0][j] = 1;
    for(int j=0;j<n+2;j++) maze[n+1][j] = 1;
}

void Print(int m,int n)              //打印迷宫
{
    for(int i=0;i<m+2;i++)
    {
        for(int j=0;j<n+2;j++)
        {
            if(maze[i][j] == 0)
              cout << "  ";
            if(maze[i][j] == 1)
              cout << "□";
        }
        cout << endl;
    }

}

typedef struct    //定义当前位置坐标
{
    int x;
    int y;
    int d;   //移动方向
}Point;

typedef struct    //定义顺序栈
{
    Point data[MAXSIZE];  //定义顺序栈的存储类型为结构体data
    int top;
}SqStack;

bool MazePath(int x1,int y1,int m, int n)
{
    int i,j,d,find;
    SqStack S;
    S.top = -1;  //初始化栈
    S.top++;
    S.data[S.top].x = x1; S.data[S.top].y = y1; S.data[S.top].d = -1;
    while(S.top>-1)
    {
        i = S.data[S.top].x; j = S.data[S.top].y; d = S.data[S.top].d;
        if(i==m&&j==n)
        {
            cout << "找到路径:" << endl;
            for(int i=0;i<m+2;i++)
            {
                for(int j=0;j<n+2;j++)
                {
                    if(maze[i][j] == 1)
                        cout << "□";
                    else if(maze[i][j] == 0)
                        cout << "  ";
                    else
                        cout << " *";
                }
                cout << endl;
            }
        return true;
        }


        find = 0;        //设置标识,1表示找到下一个可行路径,0表示没有找到
        while(d<4&&find==0)       //遍历4个方向:0->东,1->南,2->西,3->北
        {
            d++;
            switch(d)
            {
            case 0:
                i = S.data[S.top].x+1; j = S.data[S.top].y; break;
            case 1:
                i = S.data[S.top].x; j = S.data[S.top].y+1; break;
            case 2:
                i = S.data[S.top].x-1; j = S.data[S.top].y; break;
            case 3:
                i = S.data[S.top].x; j = S.data[S.top].y-1; break;
            }
            if(maze[i][j] == 0) find = 1;  //找到路径,标识置1,退出循环
        }

        if(find == 1)  //找到路径
        {
            S.data[S.top].d = d;  //记录当前节点往下一个节点的方向
            S.top++;              //下一个节点入栈
            S.data[S.top].x = i;
            S.data[S.top].y = j;
            S.data[S.top].d = -1;  //将下一个节点的走向置为-1
            maze[i][j] = -1;  //当前点置-1,表示走过
        }
        else  //未找到路径
        {
            maze[S.data[S.top].x][S.data[S.top].y] = 0;  //当前点置为0,表示未走过
            S.top--;  //出栈
        }
    }
    return false;
}


int main()
{
    int m,n;
    int k=1;
    while(k)
    {
      cin >> m >> n;
      CreateMaze(m,n);
      Print(m,n);
      if(!MazePath(1,1,m,n))
      {
            cout << "找不到路径"<<endl;
            k++;
      }
    else k=0;
    }
}

运行结果:(由于随机生成大概率没有通路,所以输出较小的迷宫容易出结果)

 

 写的很简陋,有不足之处请指正。

  • 33
    点赞
  • 252
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用 C 语言实现迷宫问题的完整代码: ```c #include<stdio.h> #include<stdlib.h> #include<conio.h> #define MAX_ROW 5 #define MAX_COL 5 int maze[MAX_ROW][MAX_COL] = { {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0}, }; void print_maze() { int i, j; for (i = 0; i < MAX_ROW; i++) { for (j = 0; j < MAX_COL; j++) { printf("%d ", maze[i][j]); } printf("\n"); } printf("*********\n"); } typedef struct { int row, col; } element; element stack[512]; int top = -1; void push(element item) { stack[++top] = item; } element pop() { return stack[top--]; } int is_empty() { return top == -1; } void path() { int i, row, col, next_row, next_col; element position; maze[0][0] = 2; top = 0; stack[0].row = stack[0].col = 0; while (!is_empty()) { position = pop(); row = position.row; col = position.col; printf("(%d, %d)\n", row, col); if (row == MAX_ROW - 1 && col == MAX_COL - 1) { printf("Path found!\n"); return; } if (col+1 < MAX_COL && maze[row][col+1] == 0) { // right next_row = row; next_col = col+1; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (row+1 < MAX_ROW && maze[row+1][col] == 0) { // down next_row = row+1; next_col = col; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (col-1 >= 0 && maze[row][col-1] == 0) { // left next_row = row; next_col = col-1; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } if (row-1 >= 0 && maze[row-1][col] == 0) { // up next_row = row-1; next_col = col; maze[next_row][next_col] = 2; position.row = next_row; position.col = next_col; push(position); } print_maze(); } printf("Path not found\n"); } int main() { path(); return 0; } ``` 在上述代码中,我们定义了一个 `maze` 数组来表示迷宫,0 表示可以通过的路,1 表示障碍物,2 表示走过的路程。我们使用栈来记录已经走过的路径,并通过 `push()` 和 `pop()` 操作来实现栈的基本功能。在 `path()` 函数中,我们从起点开始遍历迷宫,如果找到了终点,则输出 "Path found!",否则输出 "Path not found"。 我们可以通过调用 `print_maze()` 函数来输出迷宫的状态,以及调用 `path()` 函数来寻找迷宫的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值