我的递归功能无法正常工作

时间:2019-05-01 11:20:36

标签: c++ recursion

我需要遍历我创建的数据结构,以便找到移动最少的路径。所以我需要一个功能来完成以上操作。 http://prntscr.com/nj29v6

但是我最终得到了这个。  http://prntscr.com/nj2agz

void findSuccessPath(Node& Paths, vector < pair < int, int > >& winMoves, bool& success , Node& winingPath)
{

    // help variable to keep the current locotion
    pair <int, int> currentMove;
    bool childSuccess = false;



    if (Paths.success) 
    {

        //If shortest path found        
        if (minMoves > Paths.nMoves || minMoves == 0 ) 
        {
            success = true;
            winMoves.clear();

            currentMove.first  = Paths.x;
            currentMove.second = Paths.y;

            //cout << "Curents parent at :" << Paths.x << " " << Paths.y << endl;
            winMoves.push_back(currentMove);

            minMoves = Paths.nMoves;

        }
    }
    else 
    {
        //cout << "parent  at :" << Paths.x << " " << Paths.y << endl;
        for (int i = 0; i < Paths.nextNode.size(); i++) 
        {
            cout << i << endl;
            cout << " current Parent at :" << Paths.x << " " << Paths.y << endl;
            cout << "Child at :" << Paths.nextNode.at(i).x << " " << Paths.nextNode.at(i).y << endl;
            findSuccessPath(Paths.nextNode.at(i) ,  winMoves , childSuccess ,  winingPath);
            //cout << "child at " << Paths.nextNode.at(i).x << " " << Paths.nextNode.at(i).y << endl;

            if (childSuccess) 
            {


                currentMove.first  = Paths.x;
                currentMove.second = Paths.y;

                winMoves.push_back(currentMove);
                success = true;
            }

            //Initialize Success Indicator to call the next child move
            childSuccess = false;
        }
    }
}

因此,总结我的问题是我的递归函数,而不是深入了解数据结构(树),而是在for循环中保持迭代,当循环结束时进入树的下一层。

0 个答案:

没有答案