-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanoii.vbs
74 lines (65 loc) · 1.46 KB
/
hanoii.vbs
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
Class Stack
Private mSize
Private mPos
Private mArr()
Private Sub Class_Initialize
mSize = 0
mPos = -1
End Sub
Private Sub Class_Terminate
End Sub
Public Function Init(byval n)
mSize = n
mPos = -1
Redim mArr(mSize)
Set Init = Me
End Function
Public Sub Push(byval v)
if mPos+1 >= mSize Then
Wscript.Echo "Stack is full."
Else
mPos = mPos+1
mArr(mPos) = v
End If
End Sub
Public Function Pop
Dim v
If mPos > -1 Then
v = mArr(mPos)
mPos = mPos-1
Else
v = -1
Wscript.echo "Stack is empty."
End If
Pop = v
End Function
Public Function isEmpty
isEmpty = (mPos < 0)
End Function
End Class
Sub Hanoii(n, src, dest, aux)
Dim i, j, ssrc, sdest, psrc, pdest
If n mod 2 = 0 Then
j = dest : dest = aux : aux = j ' Swap aux and dest
End If
j = 2^n-1
sdest ="a" : pdest = "c"
For i = 1 to 2^n-1
If i mod 2 = 1 Then ' smallest
Wscript.echo "Move from ", sdest, " to ", pdest
ssrc = pdest : sdest = pdest
Else
Wscript.echo "Move from ", ssrc, " to ", psrc
pdest = ssrc : psrc =
Next
End Sub
Dim s : Set s = (new Stack).Init(3)
Dim j
s.Push(1)
s.Push(2)
s.Push(3)
j = s.Pop
wscript.echo j
j = s.Pop
wscript.echo j
Hanoii 3, "a", "b", "c"