|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
hanoi 塔,典型的递归问题。
…………
用数组也能做……代码变成了几十行……
几十行也不算稀奇,为了创记录,我一生气写了100多行的代码,详细分解了各个过程呢。- Sub Hanoi3()
- tms = Timer
- N = [a1]
- For i = 1 To N
- If i > 9 Then t = Chr(55 + i) & t Else t = i & t
- Next
- m = 2 ^ N
- ReDim arr(m - 1, 3)
- arr(0, 0) = N
- arr(0, 1) = t
- Dim e(3)
- Dim l(3)
- l(1) = N
-
- On Error GoTo Ext
- If N Mod 2 = 1 Then '321
- For i = 1 To m - 1 Step 6
- arr(i, 0) = "A→C"
- arr(i, 3) = arr(i - 1, 3) & 1: e(3) = 1: l(3) = l(3) + 1 'Move 1 from A to C
- arr(i, 2) = arr(i - 1, 2) 'No Change B
- arr(i, 1) = Left(arr(i - 1, 1), l(1) - 1): e(1) = Right(arr(i, 1), 1): l(1) = l(1) - 1 'Remove 1 from A
-
- arr(i + 1, 3) = arr(i, 3) 'No Change C
- If l(2) = 0 Or (l(1) > 0 And e(1) < e(2)) Then
- arr(i + 1, 0) = "A→B"
- arr(i + 1, 2) = arr(i, 2) & e(1): e(2) = e(1): l(2) = l(2) + 1 'Move k from A to B
- arr(i + 1, 1) = Left(arr(i, 1), l(1) - 1): e(1) = Right(arr(i + 1, 1), 1): l(1) = l(1) - 1 'Remove k from A
- Else
- arr(i + 1, 0) = "B→A"
- arr(i + 1, 1) = arr(i, 1) & e(2): e(1) = e(2): l(1) = l(1) + 1 'Move k from B to A
- arr(i + 1, 2) = Left(arr(i, 2), l(2) - 1): e(2) = Right(arr(i + 1, 2), 1): l(2) = l(2) - 1 'Remove k from B
- End If
-
-
- arr(i + 2, 0) = "C→B"
- arr(i + 2, 2) = arr(i + 1, 2) & 1: e(2) = 1: l(2) = l(2) + 1 'Move 1 from C to B
- arr(i + 2, 1) = arr(i + 1, 1) 'No Change A
- arr(i + 2, 3) = Left(arr(i + 1, 3), l(3) - 1): e(3) = Right(arr(i + 2, 3), 1): l(3) = l(3) - 1 'Remove 1 from C
-
- arr(i + 3, 2) = arr(i + 2, 2) 'No Change B
- If l(3) = 0 Or (l(1) > 0 And e(1) < e(3)) Then
- arr(i + 3, 0) = "A→C"
- arr(i + 3, 3) = arr(i + 2, 3) & e(1): e(3) = e(1): l(3) = l(3) + 1 'Move k from A to C
- arr(i + 3, 1) = Left(arr(i + 2, 1), l(1) - 1): e(1) = Right(arr(i + 3, 1), 1): l(1) = l(1) - 1 'Remove k from A
- Else
- arr(i + 3, 0) = "C→A"
- arr(i + 3, 1) = arr(i + 2, 1) & e(3): e(1) = e(3): l(1) = l(1) + 1 'Move k from C to A
- arr(i + 3, 3) = Left(arr(i + 2, 3), l(3) - 1): e(3) = Right(arr(i + 3, 3), 1): l(3) = l(3) - 1 'Remove k from C
- End If
-
-
- arr(i + 4, 0) = "B→A"
- arr(i + 4, 1) = arr(i + 3, 1) & 1: e(1) = 1: l(1) = l(1) + 1 'Move 1 from B to A
- arr(i + 4, 3) = arr(i + 3, 3) 'No Change C
- arr(i + 4, 2) = Left(arr(i + 3, 2), l(2) - 1): e(2) = Right(arr(i + 4, 2), 1): l(2) = l(2) - 1 'Remove 1 from B
-
- arr(i + 5, 1) = arr(i + 4, 1) 'No Change A
- If l(3) = 0 Or (l(2) > 0 And e(2) < e(3)) Then
- arr(i + 5, 0) = "B→C"
- arr(i + 5, 3) = arr(i + 4, 3) & e(2): e(3) = e(2): l(3) = l(3) + 1 'Move k from B to C
- arr(i + 5, 2) = Left(arr(i + 4, 2), l(2) - 1): e(2) = Right(arr(i + 5, 2), 1): l(2) = l(2) - 1 'Remove k from B
- Else
- arr(i + 5, 0) = "C→B"
- arr(i + 5, 2) = arr(i + 4, 2) & e(3): e(2) = e(3): l(2) = l(2) + 1 'Move k from C to B
- arr(i + 5, 3) = Left(arr(i + 4, 3), l(3) - 1): e(3) = Right(arr(i + 5, 3), 1): l(3) = l(3) - 1 'Remove k from C
- End If
- Next
-
- Else '231
- For i = 1 To m - 1 Step 6
- arr(i, 0) = "A→B"
- arr(i, 2) = arr(i - 1, 2) & 1: e(2) = 1: l(2) = l(2) + 1 'Move 1 from A to B
- arr(i, 3) = arr(i - 1, 3) 'No Change C
- arr(i, 1) = Left(arr(i - 1, 1), l(1) - 1): e(1) = Right(arr(i, 1), 1): l(1) = l(1) - 1 'Remove 1 from A
-
- arr(i + 1, 2) = arr(i, 2) 'No Change B
- If l(3) = 0 Or (l(1) > 0 And e(1) < e(3)) Then
- arr(i + 1, 0) = "A→C"
- arr(i + 1, 3) = arr(i, 3) & e(1): e(3) = e(1): l(3) = l(3) + 1 'Move k from A to C
- arr(i + 1, 1) = Left(arr(i, 1), l(1) - 1): e(1) = Right(arr(i + 1, 1), 1): l(1) = l(1) - 1 'Remove k from A
- Else
- arr(i + 1, 0) = "C→A"
- arr(i + 1, 1) = arr(i, 1) & e(3): e(1) = e(3): l(1) = l(1) + 1 'Move k from C to A
- arr(i + 1, 3) = Left(arr(i, 3), l(3) - 1): e(3) = Right(arr(i + 1, 3), 1): l(3) = l(3) - 1 'Remove k from C
- End If
-
-
- arr(i + 2, 0) = "B→C"
- arr(i + 2, 3) = arr(i + 1, 3) & 1: e(3) = 1: l(3) = l(3) + 1 'Move 1 from B to C
- arr(i + 2, 1) = arr(i + 1, 1) 'No Change A
- arr(i + 2, 2) = Left(arr(i + 1, 2), l(2) - 1): e(2) = Right(arr(i + 2, 2), 1): l(2) = l(2) - 1 'Remove 1 from B
-
- arr(i + 3, 3) = arr(i + 2, 3) 'No Change C
- If l(2) = 0 Or (l(1) > 0 And e(1) < e(2)) Then
- arr(i + 3, 0) = "A→B"
- arr(i + 3, 2) = arr(i + 2, 2) & e(1): e(2) = e(1): l(2) = l(2) + 1 'Move k from A to B
- arr(i + 3, 1) = Left(arr(i + 2, 1), l(1) - 1): e(1) = Right(arr(i + 3, 1), 1): l(1) = l(1) - 1 'Remove k from A
- Else
- arr(i + 3, 0) = "B→A"
- arr(i + 3, 1) = arr(i + 2, 1) & e(2): e(1) = e(2): l(1) = l(1) + 1 'Move k from B to A
- arr(i + 3, 2) = Left(arr(i + 2, 2), l(2) - 1): e(2) = Right(arr(i + 3, 2), 1): l(2) = l(2) - 1 'Remove k from B
- End If
-
-
- arr(i + 4, 0) = "C→A"
- arr(i + 4, 1) = arr(i + 3, 1) & 1: e(1) = 1: l(1) = l(1) + 1 'Move 1 from C to A
- arr(i + 4, 2) = arr(i + 3, 2) 'No Change B
- arr(i + 4, 3) = Left(arr(i + 3, 3), l(3) - 1): e(3) = Right(arr(i + 4, 3), 1): l(3) = l(3) - 1 'Remove 1 from C
-
- arr(i + 5, 1) = arr(i + 4, 1) 'No Change A
- If l(3) = 0 Or (l(2) > 0 And e(2) < e(3)) Then
- arr(i + 5, 0) = "B→C"
- arr(i + 5, 3) = arr(i + 4, 3) & e(2): e(3) = e(2): l(3) = l(3) + 1 'Move k from B to C
- arr(i + 5, 2) = Left(arr(i + 4, 2), l(2) - 1): e(2) = Right(arr(i + 5, 2), 1): l(2) = l(2) - 1 'Remove k from B
- Else
- arr(i + 5, 0) = "C→B"
- arr(i + 5, 2) = arr(i + 4, 2) & e(3): e(2) = e(3): l(2) = l(2) + 1 'Move k from C to B
- arr(i + 5, 3) = Left(arr(i + 4, 3), l(3) - 1): e(3) = Right(arr(i + 5, 3), 1): l(3) = l(3) - 1 'Remove k from C
- End If
- Next
-
- End If
-
- Ext:
- [g1].CurrentRegion = ""
- [g1].Resize(m, 4) = arr
- [j1] = Timer - tms
- End Sub
复制代码 |
|