没有递归

时间:2009-10-06 09:50:07

标签: c# visual-studio algorithm recursion

我写了一个递归函数。出于某种原因,它在第一次运行后才会自行调用。它只需要循环中的下一个项目而不会更深入。我正在使用Visual Studio 10,而且我睡不着觉。

public IEnumerable<string> GeneratePath(int depth)
{
    int presentDepth = depth;
    char currentPosition = this.workingPath[presentDepth];
    presentDepth++;

    foreach (char nextPosition in this.moveTable.GetPossibleNextPositions(currentPosition))
    {
        this.workingPath[presentDepth] = nextPosition;

        if (presentDepth < (ChessPiece.targetDepth - 1))
        {
            Console.WriteLine("At least this wasn't ignored:{0}", new string(this.workingPath));
            this.GeneratePath(presentDepth);
        }
        else
            yield return new string(this.workingPath);
    }
}

1 个答案:

答案 0 :(得分:8)

this.GeneratePath(presentDepth);

应该是

foreach (var i in this.GeneratePath(presentDepth))
{
  yield return ...
}