小白c++关于递归求迷宫的问题,求大神解答

qq_32828673 2015-11-15 03:16:22
今天看完数据结构,打算写一个递归求迷宫路径的函数,结果运行出错,不知道是哪里写错了,求大神解答!!

我是用个7*7的二维数组当迷宫1是通,0是不通;[1][1]是入口[6][6]是出口
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 0 0 0 1 0
0 1 1 1 0 1 0
0 1 0 1 0 0 0
0 1 0 1 1 1 0
0 0 0 0 0 0 0


下面是代码

#include <iostream>
#include <vector>
using namespace std;

struct Loca //坐标的结构体
{
int x;
int y;
};

void output(vector<Loca> x1) { //把存放路径的vector容器输出的函数
for (int i = 0; i<x1.size(); i++) {
cout << "x:" << x1[i].x << "y:" << x1[i].y << endl;
}
}

void Find(Loca start, int a[7][7], vector<Loca> &b) { //递归求路径,start是一个通的坐标,a是数组地图,b是放路径的
if (start.x == 6 && start.y ==6) output(b); //如果坐标到【6】【6】的话,说明到出口了,输出路径
if (a[++start.x][start.y] = 1) { b.push_back(start); Find(start, a, b); } //检测x++后是否为通,满足继续递归
if (a[start.x][++start.y] = 1) { b.push_back(start); Find(start, a, b); } //检测y++后是否为通,满足继续递归
}

int main(int argc, char *argv[])
{
int a[7][7] = { {0,0,0,0,0,0,0} ,{ 0,1,1,1,1,1 ,0},{ 0,1,0,0,0,1,0 },{0,1,1,1,0,1,0},{ 0,1,0,1,0,0,0 },{ 0,1,0,1,1,1,0 }, {0,0,0,0,0,0,0} };
vector<Loca> x1;
Loca a1;
a1.x = 1; a1.y = 1;
x1.push_back(a1);
Find(a1, a, x1);
return 0;
}
...全文
249 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-16
  • 打赏
  • 举报
回复
仅供参考:
/**
 * @Title  老鼠走迷宫的拓展探究
 * @Author 孙琨
 * @Date   2013-11-16
 * @At     XUST
 * @All Copyright by 孙琨
 *
 */

#include <iostream>
using namespace std;

int maze[9][9] = { // 初始化迷宫,英文maze为“迷宫”
    {2,2,2,2,2,2,2,2,2},
    {2,0,0,0,0,0,0,0,2},
    {2,0,2,2,0,2,2,0,2},
    {2,0,2,0,0,2,0,0,2},
    {2,0,2,0,2,0,2,0,2},
    {2,0,0,0,0,0,2,0,2},
    {2,2,0,2,2,0,2,2,2},
    {2,0,0,0,0,0,0,0,2},
    {2,2,2,2,2,2,2,2,2}
};

int startI = 1,startJ = 1; // 入口行列坐标
int endI = 7,endJ = 7;     // 出口行列坐标

void visit(int i,int j)  // 自动搜寻路径
{
    int m,n;

    maze[i][j] = 1;

    if((i == endI) && (j == endJ))
    {
        cout << endl << "显示路径:" << endl;
        for(m=0; m<9; m++)
        {
            for(n=0; n<9; n++)
            {
                if(maze[m][n] == 2)
                    cout << "■";
                else if(maze[m][n] == 1)
                    cout << "♀";
                else
                    cout << "  ";
            }
            cout << endl;
        }
    }

    if(maze[i][j+1] == 0)
        visit(i,j+1);
    if(maze[i+1][j] == 0)
        visit(i+1,j);
    if(maze[i][j-1] == 0)
        visit(i,j-1);
    if(maze[i-1][j] == 0)
        visit(i-1,j);

    maze[i][j] = 0;

}

int main(void)
{
    int i,j;

    cout << "显示迷宫: " << endl;
    for(i=0; i<9; i++)
    {
        for(j=0; j<9; j++)
        {
            if(maze[i][j] == 2)
                cout << "■" ;
            else
                cout << "  " ;
        }
        cout << endl;
    }

    visit(startI,startJ);

    return 0;
}

fly_dragon_fly 2015-11-16
  • 打赏
  • 举报
回复
大约框架是这样, if(a[start.x][start.y]==1){ b.push_back.. find.. b.pop_back } 应该是四个方向走吧
iyomumx 2015-11-15
  • 打赏
  • 举报
回复
== 1不是 = 1

65,183

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧