2

我有一个递归迷宫求解器算法,可以成功地通过迷宫。唯一的问题是我找不到保存起点和终点之间最短路径的方法。如何保存最短路径的坐标?

这是递归函数

void Solve_Maze(int coorx,int coory) {
    if((coorx>=0)&&(coorx<l)&&(coory>=0)&&(coory<w)) {
        if((Map[coorx][coory]==Start)||(Map[coorx][coory]==path)) {
            Map[coorx][coory]=visited;
            Solve_Maze(coorx+1,coory);
            Solve_Maze(coorx-1,coory);
            Solve_Maze(coorx,coory+1);
            Solve_Maze(coorx,coory-1);
        }else if(Map[coorx][coory]==End) {
            delete Map;
            Solved=true;
        }
    }
}

添加一个向量来存储坐标后,我得到了这个输出

(1,2)
(2,2)
(3,2)
(4,2)
(5,2)
(6,2)
(7,2)
(8,2)
(9,2)
(7,3)
(7,4)
(7,5)
(7,6)
(8,6)
(8,7)
(9,7)
(10,7)

它存储了所有坐标,但它甚至存储了我们不想走的路径的坐标((7,2)(8,2)(9,2),然后返回(7,3))。有没有办法可以只存储最短路径?

4

2 回答 2

2

以下是使用向量跟踪解坐标的方法:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

const int w = 10, l = 10;
const char Start = 'S', Path = ' ', End = 'E', Visited = '.', Solution = '*';

char Map[w][l + 1] =
{
    { "  #  # #  " },
    { " S   #  # " },
    { " ###      " },
    { "   #   #  " },
    { "    #   # " },
    { "#        #" },
    { "    ###   " },
    { "  ##E ##  " },
    { "  #       " },
    { "   ##### #" },
};

int Solved = false;
vector<pair<int,int> > SolutionCoors;

void PrintMap()
{
    int x, y;

    for (y = 0; y < w; y++)
    {
        for (x = 0; x < l; x++)
            cout << Map[y][x];

        cout << endl;
    }
}

void Solve_Maze(int CoorX, int CoorY)
{
    if (CoorX >= 0 && CoorX < l && CoorY >= 0 && CoorY < w && !Solved)
    {
        SolutionCoors.push_back(make_pair(CoorX, CoorY)); // Store potential solution

        if (Map[CoorY][CoorX] == Start || Map[CoorY][CoorX] == Path)
        {
            Map[CoorY][CoorX] = Visited;

            Solve_Maze(CoorX + 1, CoorY);
            Solve_Maze(CoorX - 1, CoorY);
            Solve_Maze(CoorX, CoorY + 1);
            Solve_Maze(CoorX, CoorY - 1);
        }
        else if (Map[CoorY][CoorX] == End)
            Solved = true;

        if (!Solved)
            SolutionCoors.pop_back();
    }
}

int main()
{
    PrintMap();

    Solve_Maze(1, 1);

    if (Solved)
    {
        for (vector<pair<int,int> >::iterator it = SolutionCoors.begin();
             it != SolutionCoors.end();
             it++)
        {
            cout << "(" << it->first << "," << it->second << ")" << endl; // Print solution coords
            Map[it->second][it->first] = Solution; // Also mark on the map
        }

        PrintMap();
    }

    return 0;
}

输出:

  #  # #  
 S   #  # 
 ###      
   #   #  
    #   # 
#        #
    ###   
  ##E ##  
  #       
   ##### #
(1,1)
(2,1)
(3,1)
(4,1)
(4,2)
(5,2)
(6,2)
(6,3)
(5,3)
(5,4)
(6,4)
(7,4)
(7,5)
(8,5)
(8,6)
(9,6)
(9,7)
(8,7)
(8,8)
(7,8)
(6,8)
(5,8)
(4,8)
(4,7)
  #  #.#..
 ****#..#.
 ###***...
   #.**#..
    #***#.
#      **#
    ### **
  ##* ##**
  #.*****.
   ##### #

另请注意,我Map[][]的坐标颠倒了。

于 2012-05-20T06:49:56.113 回答
0

您需要明确堆栈,现在它在过程Solve_Maze调用中是隐式的,并在到达时从堆栈复制到解决方案向量Solved=true,如果当前路径的长度小于先前保存的长度(如果有)。当然coorx,coory是在进入程序的时候push,退出的时候pop(不用关心状态变化)。

除了类型为“stack”的外部变量(如果不使用 std 库,您可以使用数组)之外,您可以将 coorx,coory 绑定在一个结构中,并在分配到 Solve_Maze 时传递指向它们的指针。但这大大改变了代码:在堆栈中传递要容易得多......

于 2012-05-20T05:35:56.017 回答