我一直在努力想办法解决我遇到的汉诺塔的一个变体,它有一个限制,即每一步都应该涉及中间 Hook 。下面是没有约束的普通汉诺塔的代码:
public void Transfer(int numberOfDisks, Stack<int>[] pegs, int @from, int to, int use)
{
if (numberOfDisks > 0)
{
Transfer(numberOfDisks - 1, pegs, @from, use, to);
pegs[use].Push(pegs[@from].Peek());
pegs[@from].Pop();
Console.WriteLine("Move disk: " + numberOfDisks + " from P" + @from + " to P" + to);
Transfer(numberOfDisks - 1, pegs, use, to, @from);
}
}
最佳答案
1. move smallest for 2 moves in 1 direction.
2. move other token (only possible token) by 1 move
3. goto point 1
关于algorithm - 如何解决这个汉诺塔变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128357/