forked from rsdn/nemerle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanoi2.n
70 lines (62 loc) · 1.4 KB
/
hanoi2.n
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
using Nemerle.IO;
module Hanoi {
mutable tops : array [array [int]];
mutable numbers : array [int];
mutable n : int;
mutable move_no : int;
show_top (k : int, n : int) : void
{
if (tops[k][n] == 0) printf (" | ")
else printf ("(%d)", tops[k][n]);
printf (" ")
}
show_tops () : void
{
for (mutable i = n; i >= 1; i = i - 1) {
show_top (1, i);
show_top (2, i);
show_top (3, i);
printf ("\n")
}
}
move (a : int, b : int) : void
{
printf ("%d -(%d)-> %d\n", a, move_no, b);
tops[b][numbers[b]] = tops[a][numbers[a]-1];
tops[a][numbers[a]-1] = 0;
numbers[a] = numbers[a] - 1;
numbers[b] = numbers[b] + 1;
move_no = move_no + 1;
show_tops ();
}
hanoi (n : int, a : int, b : int, c : int) : void
{
when (n > 0) {
hanoi (n - 1, a, c, b);
move (a, b);
hanoi (n - 1, c, b, a);
}
}
init () : void
{
printf ("the tower of hanoi (please type the size ?) ");
scanf ("%d", n);
numbers = array (n + 1);
tops = array [null, array (n + 1), array (n + 1), array (n + 1)];
for (mutable i = 1; i <= n; i = i + 1) {
tops[1][n - i + 1] = i;
tops[2][n - i + 1] = 0;
tops[3][n - i + 1] = 0;
};
numbers[1] = n + 1;
numbers[2] = 1;
numbers[3] = 1;
move_no = 1;
show_tops ();
}
Main () : void
{
init ();
hanoi (n, 1, 3, 2);
}
}