-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHanoiOfTower2.cs
More file actions
76 lines (67 loc) · 2.04 KB
/
HanoiOfTower2.cs
File metadata and controls
76 lines (67 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HanoiOfTower2
{
class HanoiOfTower2
{
private static List<int> peg1 = new List<int>();
private static List<int> peg2 = new List<int>();
private static List<int> peg3 = new List<int>();
private static int TESTSIZE = 8;
static void Main(string[] args)
{
for (int i = 0; i < TESTSIZE; i++)
peg1.Add(TESTSIZE - i);
//peg1.RemoveAt(1);
//peg1.Add(9);
//int j = 1;
move(7, peg1, peg2, peg3);
int k = 2;
k++;
}
/**
* Latest update: July 2, 2015
* Problem statement:
*
*
*/
private static void move(int n, List<int> source, List<int> dest, List<int> tmp)
{
if (n == 0)
moveOne(source, dest);
else
{
move(n - 1, source, tmp, dest);
if (source.Count > 1)
{
System.Console.WriteLine("My algorithm is wrong");
}
moveOne(source, dest);
move(n - 1, tmp, dest, source);
}
}
private static void moveOne( List<int> source, List<int> dest)
{
int size = source.Count;
if (size > 0)
{
int value = source[size-1];
source.RemoveAt(size-1); // only remove from the end of the list
// enforce the rule: the value added has to be smaller than the current ones
int test = dest.Count;
if (test> 0)
{
if (dest[test - 1] < value)
{
System.Console.WriteLine("Something is wrong");
return;
}
}
dest.Add(value);
}
}
}
}