|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
我的递归计算,也只要0.几秒:
- Dim a&(), b(), k&, m&, n&
- Sub test2()
- Dim i&, j&, tms#
- tms = Timer
- ReDim a&(1 To 4, 1 To 4), b(28800)
-
- m = 0: n = 2: k = 0
- Call dg2(0)
-
- MsgBox Format(Timer - tms, "0.00s ") & k
- End Sub
- Sub dg2(t&)
- Dim i&, j&, s$
- ' [a1].Resize(4, 4) = a
- If t = 7 Then k = k + 1: b(k) = a: Exit Sub
- If m < 2 Then
- For i = 1 To 4
- For j = 1 To 2
- If a(i, j) = 0 Then
- If a(i, j + 1) = 0 Then
- If a(i, j + 2) = 0 Then m = m + 1: a(i, j) = m: a(i, j + 1) = m: a(i, j + 2) = m: Call dg2(t + 1): a(i, j) = 0: a(i, j + 1) = 0: a(i, j + 2) = 0: m = m - 1
- End If
- End If
- Next
- Next
- For i = 1 To 2
- For j = 1 To 4
- If a(i, j) = 0 Then
- If a(i + 1, j) = 0 Then
- If a(i + 2, j) = 0 Then m = m + 1: a(i, j) = m: a(i + 1, j) = m: a(i + 2, j) = m: Call dg2(t + 1): a(i, j) = 0: a(i + 1, j) = 0: a(i + 2, j) = 0: m = m - 1
- End If
- End If
- Next
- Next
- Else
- If n < 7 Then
- For i = 1 To 4
- For j = 1 To 3
- If a(i, j) = 0 Then
- If a(i, j + 1) = 0 Then n = n + 1: a(i, j) = n: a(i, j + 1) = n: Call dg2(t + 1): a(i, j) = 0: a(i, j + 1) = 0: n = n - 1
- End If
- Next
- Next
- For i = 1 To 3
- For j = 1 To 4
- If a(i, j) = 0 Then
- If a(i + 1, j) = 0 Then n = n + 1: a(i, j) = n: a(i + 1, j) = n: Call dg2(t + 1): a(i, j) = 0: a(i + 1, j) = 0: n = n - 1
- End If
- Next
- Next
- End If
- End If
- End Sub
复制代码
递归计算原理:
先安排长度3的2个、且按照横、竖分开循环。
再安排长度2的5个、也按照横、竖分开循环。
这样就没有太多冗余的检查了,所以速度很快。
|
评分
-
1
查看全部评分
-
|