我写了一个递归函数。由于某种原因,它在第一次运行后不会调用自己。它只是从循环中取出下一个项目,而不深入。我正在使用 Visual Studio 10,而且 sleep 不足。
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);
}
}
最佳答案
this.GeneratePath(presentDepth);
应该是
foreach (var i in this.GeneratePath(presentDepth))
{
yield return ...
}
关于c# - 递归失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1524619/