|
- Option Explicit
- Dim map() As Long
- Dim path() As Variant
- Private Sub cmdCount_Click()
- Dim n&, x&, y&, x1&, y1&, k&, p&
- n = Range("m2")
-
- Range("p4") = count(n, map, path)
-
- Cells(n, n).Interior.Color = RGB(225, 0, 0) Xor RGB(0, 155, 21)
- x = n - 1
- x1 = n - 1
- For k = 2 * n - 2 To 1 Step -1
- p = path(k)(x)(x1)
-
- If (p = 2) Or (p = 3) Then x = x - 1
- y = k - 1 - x
- Cells(y + 1, x + 1).Interior.Color = RGB(225, 0, 0)
- If (p = 1) Or (p = 3) Then x1 = x1 - 1
- y1 = k - 1 - x1
- Cells(y1 + 1, x1 + 1).Interior.Color = RGB(0, 155, 21) Xor Cells(y1 + 1, x1 + 1).Interior.Color
- Next
- End Sub
- Private Sub cmdStat_Click()
- Dim n&, num&, max&, i&, j&
- Range("a1:j10").ClearContents
- Range("a1:j10").Interior.Pattern = xlPatternNone
- n = Range("m2")
- num = Range("n2")
- max = Range("o2")
- If n < 1 Or n > 15 Then MsgBox "格子个数超出范围": Exit Sub
- If num > n * n Then MsgBox "个数超出范围": Exit Sub
- createMap n, num, max, map
- For i = 0 To n - 1
- For j = 0 To n - 1
- If map(i, j) > 0 Then Cells(j + 1, i + 1) = map(i, j)
- Next
- Next
- End Sub
- Sub createMap(n As Long, num As Long, max As Long, map() As Long)
- Dim temp&(), i&, r&, s&, last&, m&
- last = n * n - 1
- ReDim temp(0 To last)
- ReDim map(0 To n, 0 To n)
- For i = 0 To last
- temp(i) = i
- Next
- VBA.Randomize
- For i = 0 To num - 1
- s = Int(max * Rnd + 1)
- r = Int(Rnd * (last - i))
- m = temp(r)
- map(m Mod n, m \ n) = s
- temp(r) = temp(last - i)
- Next
- End Sub
- Function count(n As Long, map() As Long, path() As Variant) As Long
-
- Dim k&, x&, y&, x1&, y1&, xs&, xe&, last&, cur&, before&
- Dim p() As Variant
- Dim p1() As Byte
- Dim state() As Long
-
- last = n * 2 - 2
- ReDim path(0 To last)
- ReDim state(1, n - 1, n - 1)
- state(0, 0, 0) = map(0, 0)
- before = 0
-
- For k = 1 To last
- If k < n Then
- xs = 0
- xe = k
- Else
- xs = k - (n - 1)
- xe = n - 1
- End If
- ReDim p(xs To xe)
-
- Dim s1&, s&, s0&, sMax&
-
- cur = before Xor 1
-
- For x = xs To xe
- ReDim p1(x To xe)
- y = k - x
- s = map(x, y)
-
- For x1 = x To xe
-
- sMax = 0
- y1 = k - x1
- s1 = map(x1, y1)
- If y > 0 Then
- If x1 > 0 And x <> x1 Then
- s0 = state(before, x, x1 - 1) + s + s1
- If s0 >= sMax Then
- sMax = s0
- p1(x1) = 1
- End If
- End If
- If y1 > 0 Then
- s0 = state(before, x, x1) + s
- If x <> x1 Then s0 = s0 + s1
- If s0 >= sMax Then
- sMax = s0
- p1(x1) = 0
- End If
- End If
- End If
- If x > 0 Then
- If x1 > 0 Then
- s0 = state(before, x - 1, x1 - 1) + s
- If x <> x1 Then s0 = s0 + s1
- If s0 >= sMax Then
- sMax = s0
- p1(x1) = 3
- End If
- End If
- If y1 > 0 Then
- s0 = state(before, x - 1, x1) + s
- If x <> x1 Then s0 = s0 + s1
- If s0 >= sMax Then
- sMax = s0
- p1(x1) = 2
- End If
- End If
- End If
- state(cur, x, x1) = sMax
-
- Next
- p(x) = p1
- Next
-
- path(k) = p
- before = cur
- Next
-
- count = state(before, x - 1, x1 - 1)
-
- End Function
复制代码
|
评分
-
3
查看全部评分
-
|