-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.scala
219 lines (200 loc) · 7.15 KB
/
TowerOfHanoi.scala
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import scala.collection.mutable.ListBuffer
/**
* Created by ken.j.zheng on 6/5/2018.
*/
object TowerOfHanoi extends App {
def splitDiskByWaterMark(list:ListBuffer[(Int,Int)], waterMark:Int):(ListBuffer[(Int,Int)],ListBuffer[(Int,Int)])={
val list1 = list.take(waterMark)
val list2 = list.drop(waterMark)
(list1,list2)
}
def getIndexOfBaseDisk(list:ListBuffer[(Int,Int)]):Int = {
var init = list.last._2
for (i <- list.length-1 to 0 by -1){
if(init-list(i)._2>1)
return i+1
else
init = list(i)._2
}
-1
}
def moveDisk(tower1:ListBuffer[(Int,Int)],
tower2:ListBuffer[(Int,Int)],
tower3:ListBuffer[(Int,Int)],
direction:(String,String)
): (ListBuffer[(Int,Int)],ListBuffer[(Int,Int)],ListBuffer[(Int,Int)]) = {
var x = tower1
var y = tower2
var z = tower3
println(direction._1 + "->" + direction._2,x.map(m => {m._1}),y.map(m => {m._1}),z.map(m => {m._1}))
val output = direction match {
case (from,to) => {
val output = (from,to) match {
case ("TOWER1","TOWER3") => {
if(x.length==2 || (x.length>2 && x(x.length-2)._2 - x(x.length-3)._2>1)){
y.append(x.last)
x = x.dropRight(1)
z.append(x.last)
x = x.dropRight(1)
z.append(y.last)
y = y.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(x)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(x,waterMark)
val (x1, y1, z1) = moveDisk(l2.tail, y, z, ("TOWER1", "TOWER2"))
z1.append(l2.head)
moveDisk(l1 ++ x1, y1, z1,("TOWER2", "TOWER3") )
}
else {
val (x1, y1, z1) = moveDisk(x.tail, y, z, ("TOWER1", "TOWER2"))
z1.append(x.head)
moveDisk(x1, y1, z1, ("TOWER2", "TOWER3"))
}
}
}
case ("TOWER1", "TOWER2") => {
if(x.length==2 || (x.length>2 && x(x.length-2)._2 - x(x.length-3)._2>1)){
z.append(x.last)
x = x.dropRight(1)
y.append(x.last)
x = x.dropRight(1)
y.append(z.last)
z = z.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(x)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(x,waterMark)
val (x1, y1, z1) = moveDisk(l2.tail, y, z, ("TOWER1", "TOWER3"))
y1.append(l2.head)
moveDisk(l1 ++ x1,y1,z1,("TOWER3", "TOWER2"))
}
else {
val (x1, y1, z1) = moveDisk(x.tail, y, z, ("TOWER1", "TOWER3"))
y1.append(x.head)
moveDisk(x1, y1, z1, ("TOWER3", "TOWER2"))
}
}
}
case ("TOWER2", "TOWER1") => {
if(y.length==2 || (y.length>2 && y(y.length-2)._2 - y(y.length-3)._2>1)){
z.append(y.last)
y = y.dropRight(1)
x.append(y.last)
y = y.dropRight(1)
x.append(z.last)
z = z.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(y)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(y,waterMark)
val (x1, y1, z1) = moveDisk(x, l2.tail, z, ("TOWER2", "TOWER3"))
x1.append(l2.head)
moveDisk(x1, l1 ++ y1, z1, ("TOWER3", "TOWER1"))
}
else {
val (x1, y1, z1) = moveDisk(x, y.tail, z, ("TOWER2", "TOWER3"))
x1.append(y.head)
moveDisk(x1, y1, z1, ("TOWER3", "TOWER1"))
}
}
}
case ("TOWER2","TOWER3") => {
if(y.length==2 || (y.length>2 && y(y.length-2)._2 - y(y.length-3)._2>1)){
x.append(y.last)
y = y.dropRight(1)
z.append(y.last)
y = y.dropRight(1)
z.append(x.last)
x = x.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(y)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(y,waterMark)
val (x1, y1, z1) = moveDisk(x, l2.tail, z, ("TOWER2", "TOWER1"))
z1.append(l2.head)
moveDisk(x1, l1 ++ y1, z1, ("TOWER1", "TOWER3"))
}
else {
val (x1, y1, z1) = moveDisk(x, y.tail, z, ("TOWER2", "TOWER1"))
z1.append(y.head)
moveDisk(x1, y1, z1, ("TOWER1", "TOWER3"))
}
}
}
case ("TOWER3", "TOWER2") => {
if(z.length==2 || (z.length>2 && z(z.length-2)._2 - z(z.length-3)._2>1)){
x.append(z.last)
z = z.dropRight(1)
y.append(z.last)
z = z.dropRight(1)
y.append(x.last)
x = x.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(z)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(z,waterMark)
val (x1, y1, z1) = moveDisk(x, y, l2.tail, ("TOWER3", "TOWER1"))
y1.append(l2.head)
moveDisk(x1, y1, l1 ++ z1, ("TOWER1", "TOWER2"))
}
else {
val (x1, y1, z1) = moveDisk(x, y, z.tail, ("TOWER3", "TOWER1"))
y1.append(z.head)
moveDisk(x1, y1, z1, ("TOWER1", "TOWER2"))
}
}
}
case ("TOWER3", "TOWER1") => {
if (z.length==2 || (z.length>2 && z(z.length-2)._2 - z(z.length-3)._2>1)){
y.append(z.last)
z = z.dropRight(1)
x.append(z.last)
z = z.dropRight(1)
x.append(y.last)
y = y.dropRight(1)
(x,y,z)
}
else {
val waterMark = getIndexOfBaseDisk(z)
if(waterMark != -1){
val (l1,l2) = splitDiskByWaterMark(z,waterMark)
val (x1, y1, z1) = moveDisk(x, y, l2.tail, ("TOWER3", "TOWER2"))
x1.append(l2.head)
moveDisk(x1, y1, l1 ++ z1, ("TOWER2", "TOWER1"))
}
else {
val (x1, y1, z1) = moveDisk(x, y, z.tail, ("TOWER3", "TOWER2"))
x1.append(z.head)
moveDisk(x1, y1, z1, ("TOWER2", "TOWER1"))
}
}
}
}
output
}
}
output
}
val testMethod = ListBuffer((9,1), (6,4), (3,7), (2,8), (1,9))
val a1 = getIndexOfBaseDisk(testMethod)
println(a1)
val testSample = ListBuffer(13,12,11,10,9,8,7,6,5,4,3,2,1).zipWithIndex
val test = testSample.map (m => {
(m._1,m._2+1)
})
println(test)
val t131 = ListBuffer[(Int,Int)]()
val t132 = ListBuffer[(Int,Int)]()
println(moveDisk(test,t131,t132,("TOWER1","TOWER3")))
}