今天看了WillWayer的一篇文章(http://www.cnblogs.com/willwayer/archive/2009/03/24/1420227.html),自己也写了一个,练练手。
今天看了WillWayer的一篇文章(http://www.cnblogs.com/willwayer/archive/2009/03/24/1420227.html),自己也写了一个,练练手。
Code
1 public static class Hanoi
2 {
3 public static int _floorCount;
4
5 public static int FloorCount
6 {
7 get
8 {
9 return _floorCount;
10 }
11
12 set
13 {
14 if (value < 1)
15 throw new ApplicationException("塔的层数必须是一个正整数!");
16 _floorCount = value;
17 }
18 }
19
20 static Hanoi()
21 {
22 FloorCount = 64;
23 }
24
25 public enum Seat
26 {
27 A,
28 B,
29 C
30 }
31
32 public class Step
33 {
34 public Seat Start
35 {
36 get;
37 set;
38 }
39
40 public Seat End
41 {
42 get;
43 set;
44 }
45
46 public Step(Seat start, Seat end)
47 {
48 Start = start;
49 End = end;
50 }
51
52 public override string ToString()
53 {
54 return Start.ToString() + " " + End.ToString();
55 }
56 }
57
58 private static List<Step> CalculateSteps(int floorCount, Seat start, Seat temp, Seat end)
59 {
60 List<Step> steps = new List<Step>();
61 if (floorCount == 1)
62 steps.Add(new Step(start, end));
63 else
64 {
65 foreach (Step step in CalculateSteps(floorCount - 1, start, end, temp))
66 {
67 steps.Add(step);
68 }
69 steps.Add(new Step(start, Seat.C));
70 foreach (Step step in CalculateSteps(floorCount - 1, temp, start, end))
71 {
72 steps.Add(step);
73 }
74 }
75
76 return steps;
77 }
78
79 public static List<Step> CalculateSteps()
80 {
81 return CalculateSteps(FloorCount, Seat.A, Seat.B, Seat.C);
82 }
83
84 public static void Print()
85 {
86 foreach (Step step in CalculateSteps())
87 {
88 Console.WriteLine(step);
89 }
90 }
91 }
1 public static class Hanoi
2 {
3 public static int _floorCount;
4
5 public static int FloorCount
6 {
7 get
8 {
9 return _floorCount;
10 }
11
12 set
13 {
14 if (value < 1)
15 throw new ApplicationException("塔的层数必须是一个正整数!");
16 _floorCount = value;
17 }
18 }
19
20 static Hanoi()
21 {
22 FloorCount = 64;
23 }
24
25 public enum Seat
26 {
27 A,
28 B,
29 C
30 }
31
32 public class Step
33 {
34 public Seat Start
35 {
36 get;
37 set;
38 }
39
40 public Seat End
41 {
42 get;
43 set;
44 }
45
46 public Step(Seat start, Seat end)
47 {
48 Start = start;
49 End = end;
50 }
51
52 public override string ToString()
53 {
54 return Start.ToString() + " " + End.ToString();
55 }
56 }
57
58 private static List<Step> CalculateSteps(int floorCount, Seat start, Seat temp, Seat end)
59 {
60 List<Step> steps = new List<Step>();
61 if (floorCount == 1)
62 steps.Add(new Step(start, end));
63 else
64 {
65 foreach (Step step in CalculateSteps(floorCount - 1, start, end, temp))
66 {
67 steps.Add(step);
68 }
69 steps.Add(new Step(start, Seat.C));
70 foreach (Step step in CalculateSteps(floorCount - 1, temp, start, end))
71 {
72 steps.Add(step);
73 }
74 }
75
76 return steps;
77 }
78
79 public static List<Step> CalculateSteps()
80 {
81 return CalculateSteps(FloorCount, Seat.A, Seat.B, Seat.C);
82 }
83
84 public static void Print()
85 {
86 foreach (Step step in CalculateSteps())
87 {
88 Console.WriteLine(step);
89 }
90 }
91 }