我的递归算法中发现所有最短,唯一路径的问题

时间:2019-01-28 01:39:05

标签: c++ c++11 recursion c++14 path-finding

我一直在研究一种递归算法,该算法应该找到在n x m矩阵中从点A到点B的所有最短,唯一的可能路径。目前,我的算法可以找到从单个方向开始的所有可能路径。但是,当我回到算法开始的第一个堆栈帧时,我会陷入在第一帧中执行的先前操作中所采取的任何方向。我确定这是由于我的运动功能引用了我正在使用的当前解决方案的事实。我认为需要发生的是,我需要进一步定位参数,以便从本质上讲我可以从平方开始。我只是不确定该怎么做,因为在任何时候,当我尝试不再将引用传递给运动算法时,“解决方案”都会以一个字母的形式返回。

关于使第一个堆栈帧与我的运动算法更独立的任何提示吗?

{void Robot::findTreasureHelper(Board whichBoard, int x, int y, std::string solution)

    ++callCount;

    if (x == TREASURE_X && y == TREASURE_Y)
    {
        ++numSolutions;
        return;
    }

    if (move(whichBoard, 'N', x, y, solution))
        findTreasureHelper(whichBoard, x, y, solution);
    if (move(whichBoard, 'E', x, y, solution))
        findTreasureHelper(whichBoard, x, y, solution);
    if (move(whichBoard, 'S', x, y, solution))
        findTreasureHelper(whichBoard, x, y, solution);
    if (move(whichBoard, 'W', x, y, solution))
        findTreasureHelper(whichBoard, x, y, solution);

    {bool Robot::move(Board& whichBoard, const char& whichDir, int& x, int& y, std::string& solution)

    bool didMove = false;

    int direction = 1;
    if (whichDir == 'S' || whichDir == 'E')
        direction = -1;
    int  *coordinate = &x;
    if (whichDir == 'N' || whichDir == 'S')
        coordinate = &y;

    //check bounds, consecutive movements, and whether robot has been here on this solution
    if (x == TREASURE_X && y == TREASURE_Y)
        *coordinate += direction;
    else if (*coordinate - direction >= 0 && *coordinate - direction < whichBoard.board.size() 
        && *coordinate - direction < whichBoard.board[y].size() && moveCount < CONSECUTIVE_MOVES)
    {
        *coordinate -= direction;
        //check blocks and previously been here
        if (whichBoard.board[y][x] == -1 || whichBoard.board[y][x] == currentSolutionIndex)
            *coordinate += direction;
        else if (x == TREASURE_X && y == TREASURE_Y)
        {
            switch (whichDir)
            {
            case 'N':
            case 'S':
                //check consecutive moves
                solution.back() == 'N' || solution.back() == 'S' ? ++moveCount : moveCount = 0;
                //add correct character to solution
                direction == 1 ? solution += 'N' : solution += 'S';
                break;
            case 'E':
            case 'W':
                //check consecutive moves
                solution.back() == 'E' || solution.back() == 'W' ? ++moveCount : moveCount = 0;
                //add correct character to solution
                direction == 1 ? solution += 'W' : solution += 'E';
                break;
            }
            //*coordinate += direction;
            this->solutions.push_back(solution);
            didMove = true;
        }
        else // complete the move randomly
        {
            switch (whichDir)
            {
            case 'N':
            case 'S':
                //check consecutive moves
                solution.back() == 'N' || solution.back() == 'S' ? ++moveCount : moveCount = 0;
                //add correct character to solution
                direction == 1 ? solution += 'N' : solution += 'S';
                break;
            case 'E':
            case 'W':
                //check consecutive moves
                solution.back() == 'E' || solution.back() == 'W' ? ++moveCount : moveCount = 0;
                //add correct character to solution
                direction == 1 ? solution += 'W' : solution += 'E';
                break;
            }
            didMove = true;
            whichBoard.board[y][x] = currentSolutionIndex;
        }

    }
    return didMove;
}

1 个答案:

答案 0 :(得分:1)

要找到最短的路径,我建议您使用BFS而不是DFS,以便更快地找到解决方案。可以借助队列来完成。从起始索引开始,将邻居(在您的情况下为四个邻居N,S,E,W)添加到队列中。

具有一个数据结构来跟踪所有访问的索引(最好是一个集合)。因此,一旦完成这些设置,请重复执行以下操作-

  1. 从队列中弹出元素。
  2. 检查是否这是您的目的地索引,是否已找到最短路径。
  3. 如果没有,则将该索引标记为已访问并将其邻居添加到队列中。

迭代解决方案-明显的好处:)