Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Hanoi
6{
7 class Program
8 {
9 const int HANOI_SIZE = 5; //汉诺塔规模大小
10 static int count; //盘子搬动次数
11 static Dictionary<string ,int> abc; //泛型,记录柱子上的盘子状态,如:A-54321
12
13 /**//// <summary>
14 /// 将x塔座上的按直径从小大大且自上而下编号为1至n的n个圆盘按规则搬到z上,y可作辅助塔座
15 /// </summary>
16 /// <param name="n">盘子个数</param>
17 /// <param name="x">盘子标号X</param>
18 /// <param name="y">盘子标号Y</param>
19 /// <param name="z">盘子标号Z</param>
20 static void hanoi(int n, string x, string y, string z)
21 {
22 if (n == 1)
23 {
24 move(x, 1, z); //将编号为1的盘从x移到z
25 }
26 else
27 {
28 hanoi(n - 1, x, z, y); //将x上编号为1至n-1的盘移动到y,z作为辅助
29 move(x, n, z); //将编号为n的圆盘从x移到z
30 hanoi(n - 1, y, x, z); //将y上编号为1至n-1的盘移动到z,x作为辅助
31 }
32 }
33
34 /**//// <summary>
35 /// 搬动操作,count是初值为0的全局变量,对搬动计数
36 /// 这里用整数比较巧妙的解决了圆盘状态的表示,是因为有一个特性:大数字总在小数字前面,而且后面不足五位添0
37 /// </summary>
38 /// <param name="x"></param>
39 /// <param name="n"></param>
40 /// <param name="y"></param>
41 static void move(string x, int n, string y)
42 {
43 Console.WriteLine("第" + ++count + "次移动,把盘" + n + "从" + x + " --> " + y);
44 abc[x] = abc[x] / 10; //调整柱上圆盘状态,搬出
45 abc[y] = abc[y] * 10 + n; //调整柱上圆盘状态,搬进
46 Console.WriteLine(Filter(abc["A"]) + " " + Filter(abc["B"]) + " " + Filter(abc["C"]));
47 }
48
49 /**//// <summary>
50 /// 过滤整数,补足成长度为HANOI_SIZE的字符串
51 /// </summary>
52 /// <param name="i">整数</param>
53 /// <returns>字符串</returns>
54 //static string Filter(int i)
55 //{
56 // string s = string.Empty+ i;
57 // int addZeroCount = HANOI_SIZE - s.Length;
58 // for (int j = 0; j < addZeroCount; j++)
59 // s += "0";
60 // return s;
61 //}
62 //改为:
63 static string Filter(int i)
64 {
65 return i.ToString().PadRight(HANOI_SIZE,'0');
66 }
67
68 static void Main(string[] args)
69 {
70 //初始化柱子状态,和搬动次数计数
71 abc = new Dictionary<string, int>();
72 abc["A"] = 0;
73 abc["B"] = 0;
74 abc["C"] = 0;
75 count = 0;
76
77 int size;
78 for (size = HANOI_SIZE; size > 0; size--)
79 {
80 abc["A"] = abc["A"] * 10 + size;
81 }
82
83 hanoi(HANOI_SIZE, "A", "B", "C");
84
85 Console.ReadLine();
86 }
87 }
88}
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Hanoi
6{
7 class Program
8 {
9 const int HANOI_SIZE = 5; //汉诺塔规模大小
10 static int count; //盘子搬动次数
11 static Dictionary<string ,int> abc; //泛型,记录柱子上的盘子状态,如:A-54321
12
13 /**//// <summary>
14 /// 将x塔座上的按直径从小大大且自上而下编号为1至n的n个圆盘按规则搬到z上,y可作辅助塔座
15 /// </summary>
16 /// <param name="n">盘子个数</param>
17 /// <param name="x">盘子标号X</param>
18 /// <param name="y">盘子标号Y</param>
19 /// <param name="z">盘子标号Z</param>
20 static void hanoi(int n, string x, string y, string z)
21 {
22 if (n == 1)
23 {
24 move(x, 1, z); //将编号为1的盘从x移到z
25 }
26 else
27 {
28 hanoi(n - 1, x, z, y); //将x上编号为1至n-1的盘移动到y,z作为辅助
29 move(x, n, z); //将编号为n的圆盘从x移到z
30 hanoi(n - 1, y, x, z); //将y上编号为1至n-1的盘移动到z,x作为辅助
31 }
32 }
33
34 /**//// <summary>
35 /// 搬动操作,count是初值为0的全局变量,对搬动计数
36 /// 这里用整数比较巧妙的解决了圆盘状态的表示,是因为有一个特性:大数字总在小数字前面,而且后面不足五位添0
37 /// </summary>
38 /// <param name="x"></param>
39 /// <param name="n"></param>
40 /// <param name="y"></param>
41 static void move(string x, int n, string y)
42 {
43 Console.WriteLine("第" + ++count + "次移动,把盘" + n + "从" + x + " --> " + y);
44 abc[x] = abc[x] / 10; //调整柱上圆盘状态,搬出
45 abc[y] = abc[y] * 10 + n; //调整柱上圆盘状态,搬进
46 Console.WriteLine(Filter(abc["A"]) + " " + Filter(abc["B"]) + " " + Filter(abc["C"]));
47 }
48
49 /**//// <summary>
50 /// 过滤整数,补足成长度为HANOI_SIZE的字符串
51 /// </summary>
52 /// <param name="i">整数</param>
53 /// <returns>字符串</returns>
54 //static string Filter(int i)
55 //{
56 // string s = string.Empty+ i;
57 // int addZeroCount = HANOI_SIZE - s.Length;
58 // for (int j = 0; j < addZeroCount; j++)
59 // s += "0";
60 // return s;
61 //}
62 //改为:
63 static string Filter(int i)
64 {
65 return i.ToString().PadRight(HANOI_SIZE,'0');
66 }
67
68 static void Main(string[] args)
69 {
70 //初始化柱子状态,和搬动次数计数
71 abc = new Dictionary<string, int>();
72 abc["A"] = 0;
73 abc["B"] = 0;
74 abc["C"] = 0;
75 count = 0;
76
77 int size;
78 for (size = HANOI_SIZE; size > 0; size--)
79 {
80 abc["A"] = abc["A"] * 10 + size;
81 }
82
83 hanoi(HANOI_SIZE, "A", "B", "C");
84
85 Console.ReadLine();
86 }
87 }
88}
呵呵,以上是我独家秘方,想知道个所以然,就自己复制代码,运行看看吧。呵呵。。。我懒得写啦。