如何使用递归来查找和打印从A点到B点的最短路径? (C ++)

时间:2019-02-03 07:00:34

标签: c++ recursion

这里的第一篇文章,如果我做错了什么,请原谅我。我无法弄清楚如何递归地找到并打印从A点到B点的所有最短路径。

我正在制作一个程序,该程序使用尽可能短的路径使机器人从A点移动到B点。该程序必须使用递归并找到并打印机器人从点A到点B的所有最短路径。例如,如果机器人从点(1、2)开始并且需要以某种方式指向点(3,5),那么程序应该仅用5步就打印出10条路径(例如NNNEE)。这是两个最重要的功能,对不起,代码太乱了。

void Robot::FindPath(const Board &board)
{
    string path = "";
    vector < string > pathsVector;
    MoveRobot(startingX,
              startingY,
              treasureX,
              treasureY,
              path,
              "",
              0,
              pathsVector);
}

void Robot::MoveRobot(const int &startingX,
                      const int &startingY,
                      const int &treasureX,
                      const int &treasureY,
                      const string &path,
                      const string &lastMove,
                      const int &numMovesDirection,
                      vector<string> &pathVector)
{
    if (startingX == treasureX && startingY == treasureY)
    {
        cout << path << endl;
        PathCount++;
        pathVector.push_back(path);
        return;
    }
    else if (startingY == treasureY)
    {
        if (startingX - treasureX > 0)
        {
            if (lastMove == "W" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "W" && 
                      (numMovesDirection < MaxStepsDir || 
                       numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX - 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "W",
                          "W",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "W")
            {
                MoveRobot(startingX - 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "W",
                          "W",
                          1,
                          pathVector);
            }
        }
        else if (startingX - treasureX < 0)
        {
            if (lastMove == "E" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "E" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX + 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "E",
                          "E",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "E")
            {
                MoveRobot(startingX + 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "E",
                          "E",
                          1,
                          pathVector);
            }
        }
    }
    else if (startingX == treasureX)
    {
        if (startingY - treasureY > 0)
        {
            if (lastMove == "S" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "S" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX,
                          startingY - 1,
                          treasureX,
                          treasureY,
                          path + "S",
                          "S",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "S")
            {
                MoveRobot(startingX,
                          startingY - 1,
                          treasureX,
                          treasureY,
                          path + "S",
                          "S",
                          1,
                          pathVector);
            }
        }
        else if (startingY + treasureY > 0)
        {
            if (lastMove == "N" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "N" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX,
                          startingY + 1,
                          treasureX,
                          treasureY,
                          path + "N",
                          "N",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "N")
            {
                MoveRobot(startingX,
                          startingY + 1,
                          treasureX,
                          treasureY,
                          path + "N",
                          "N",
                          1,
                          pathVector);
            }
        }
    }
    else
    {
        if (startingX - treasureX > 0)
        {
            if (lastMove == "W" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "W" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX - 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "W",
                          "W",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "W")
            {
                MoveRobot(startingX - 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "W",
                          "W",
                          1,
                          pathVector);
            }
        }
        else if (startingX - treasureX < 0)
        {
            if (lastMove == "E" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "E" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX + 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "E",
                          "E",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "E")
            {
                MoveRobot(startingX + 1,
                          startingY,
                          treasureX,
                          treasureY,
                          path + "E",
                          "E",
                          1,
                          pathVector);
            }
        }
        else if (startingY - treasureY > 0)
        {
            if (lastMove == "S" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "S" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX,
                          startingY - 1,
                          treasureX,
                          treasureY,
                          path + "S",
                          "S",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "S")
            {
                MoveRobot(startingX,
                          startingY - 1,
                          treasureX,
                          treasureY,
                          path + "S",
                          "S",
                          1,
                          pathVector);
            }
        }
        else if (startingY + treasureY > 0)
        {
            if (lastMove == "N" && numMovesDirection == MaxStepsDir)
            {

            }
            else if (lastMove == "N" && 
                     (numMovesDirection < MaxStepsDir || 
                      numMovesDirection > MaxStepsDir))
            {
                MoveRobot(startingX,
                          startingY + 1,
                          treasureX,
                          treasureY,
                          path + "N",
                          "N",
                          numMovesDirection + 1,
                          pathVector);
            }
            else if (lastMove != "N")
            {
                MoveRobot(startingX,
                          startingY + 1,
                          treasureX,
                          treasureY,
                          path + "N",
                          "N",
                          1,
                          pathVector);
            }
        }
    }
}

我知道第一个if语句中的“ return”将停止递归,但是当我找到示例中给出的所有10条最短路径并找到并打印时,我不知道在哪里结束递归。因为它只打印EENNN。请忽略if语句,它说“ if(lastMove ==“ W” && numMovesDirection == MaxStepsDir)”,我计划稍后填充这些语句,以使程序的另一部分起作用。任何帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

自己弄清楚。抱歉,如果我在此浪费任何时间。我既有对递归的基本误解,又有代码中的小错误。我没有意识到递归会通过回溯并沿着其他路径自动打印所有最短的路径。我的代码中阻止此错误的错误是,在代码末尾的大“ else”中,我无意中输入了“ else if(startingY-宝物Y> 0)”。这应该只是说“ if(startY-宝物Y> 0)”,因为如果没有一个合适的位置,那么大的else代码块的全部要点就是改变x坐标和y坐标,如果没有则将其放到其他位置那。一旦将else if更改为if,它将打印出我给出的示例的所有十个最短路径。