using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int times = new HanoiTower(3).SolveTower();
Console.WriteLine("一共需要{0}次", times);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.ReadKey();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class HanoiTower
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int plates;
public HanoiTower(int plates)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.plates = plates;
}
public int SolveTower()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return SolveTower(plates, "A", "B", "C");
}
private int SolveTower(int plates,string a, string b, string c)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int count = 0;
if (plates <= 0)
throw new ArgumentOutOfRangeException("plates","盘子数必须是大于0的整数");
if (plates == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
move(a, c);
return 1;
}
count += SolveTower(plates - 1, a, c, b);
count += SolveTower(1, a, b, c);
count += SolveTower(plates - 1, b, a, c);
return count;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
private void move(string a, string c)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(a + " => " + c);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
张旋(zxsoft)
如对本文有什么疑问,请在下面写下留言,谢谢!