如何实现递归寻路

nabasasun 2014-04-01 01:59:50
#define MAX_NUMBER_LENGTH 6
//迷宫
static int gPath[MAX_NUMBER_LENGTH][MAX_NUMBER_LENGTH] = {
{3 , 0, 0, 0, 1, 1},
{0, 1, 1, 0, 1, 2},
{0 , 1, 1, 0, 1, 0},
{0 , 1, 1, 0, 1, 0},
{0 , 1, 1, 0, 1, 0},
{0 , 0, 1, 0, 0, 0}
};

//上面的2维地图数组中,0是可行走的,1是障碍,2是终点,3是出生点位置


static int gValue[MAX_NUMBER_LENGTH][MAX_NUMBER_LENGTH] = {0}; /* 记录已走过的路 */



走过的路径用#标记出来。


...全文
246 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
/**
 * @Title  老鼠走迷宫
 * @Author 孙琨
 * @Date   2013-11-16
 * @At     XUST
 * @All Copyright by 孙琨
 *
 */

#include <iostream>
using namespace std;

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

int startI = 1,startJ = 1; // 入口行列坐标
int endI = 5,endJ = 5;     // 出口行列坐标
int success = 0;           // 判断路是否走通

int visit(int i,int j)  // 自动搜寻路径
{
    maze[i][j] = 1;

    if((i == endI) && (j == endJ))
        success = 1;

    if((success != 1) && (maze[i][j+1] == 0))  // 下
        visit(i,j+1);
    if((success != 1) && (maze[i+1][j]) == 0)  // 右
        visit(i+1,j);
    if((success != 1) && (maze[i][j-1] == 0))  // 上
        visit(i,j-1);
    if((success != 1) && (maze[i-1][j] == 0))  // 左
        visit(i-1,j);

    if(success != 1)
        maze[i][j] = 0;

    return success;

}

int main(void)
{
    int i,j;

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

    if(visit(startI,startJ) == 0)
    {
        cout << endl << "没有找到出口!" << endl;
    }
    else
    {
        cout << endl << "显示路径: " << endl;
        for(i=0; i<7; i++)
        {
            for(j=0; j<7; j++)
            {
                if(maze[i][j] == 2)
                    cout << "■";
                else if(maze[i][j] == 1)
                    cout << "♀";
                else
                    cout << "  ";
            }
            cout << endl;
        }
    }
    return 0;
}

赵4老师 2014-04-02
  • 打赏
  • 举报
回复
“给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
bobo928843007 2014-04-01
  • 打赏
  • 举报
回复
引用 6 楼 nabasasun 的回复:
求解,为什么执行了4次?
因为有4条路径吧
nabasasun 2014-04-01
  • 打赏
  • 举报
回复
求解,为什么执行了4次?

赵4老师 2014-04-01
  • 打赏
  • 举报
回复
引用 3 楼 max_min_ 的回复:
[quote=引用 1 楼 zhao4zhong1 的回复:]
/**
 * @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;
}

作者都不改下?[/quote] 这点节操偶还是要的。
derekrose 2014-04-01
  • 打赏
  • 举报
回复
引用 3 楼 max_min_ 的回复:
[quote=引用 1 楼 zhao4zhong1 的回复:]
/**
 * @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;
}

作者都不改下?[/quote]就不改!
max_min_ 2014-04-01
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
/**
 * @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;
}

作者都不改下?
nabasasun 2014-04-01
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
/**
 * @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;
}

很不错,不过为什么输入XY的时候是反过来的
赵4老师 2014-04-01
  • 打赏
  • 举报
回复
/**
 * @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;
}

64,648

社区成员

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

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