本帖最后由 wuliaolang 于 2018-9-6 21:34 编辑
试着做了一下,把自己做晕了,估计漏洞百出
一开始想起来前几年考一建时学的网络计划,和关键线路计算很类似,后面才反应过来,要求的好像不是最短路线,那么只要过程不重复就可以咯
- Public arr(100), brr(100), LR, ans, wr, ifwr 'arr记录过程,brr收集死点,LR方向,ans路线数,wr死点数
- Sub 和尚和妖怪()
- Cells.Clear
- t = Timer
- ans = 0: wr = 0: LR = 1
- m = 3: n = 3
- arr(1) = m & ";" & n & ";" & 0 & ";" & 0 & ";" & 1
- e = s(m, n, 0, 0, 1)
- Erase arr, brr
- MsgBox Format(Timer - t, "0.0000")
- End Sub
- Function s(m, n, i, j, x)
- If m = 0 And n = 0 Then
- ans = ans + 1
- Range("a" & 2 * ans - 1) = x
- Range("a" & 2 * ans) = Join(arr, "&")
- s = x
- arr(x) = ""
- LR = 1
- Exit Function
- Else
- ifans = ans
- ifwr = 1
- s = f(m - LR * 2, n, i + LR * 2, j, x + 1)
- s = f(m - LR * 1, n, i + LR * 1, j, x + 1)
- s = f(m - LR * 1, n - LR * 1, i + LR * 1, j + LR * 1, x + 1)
- s = f(m, n - LR * 2, i, j + LR * 2, x + 1)
- s = f(m, n - LR * 1, i, j + LR * 1, x + 1)
- If ifans = ans And ifwr = 1 Then
- wr = wr + 1
- brr(wr) = m & ";" & n & ";" & i & ";" & j & ";" & LR
- End If
- LR = LR * -1
- arr(x) = ""
- End If
- End Function
- Function f(m, n, i, j, x)
- If m < 0 Or n < 0 Or i < 0 Or j < 0 Then Exit Function
- If m < n And m * n > 0 Then Exit Function
- If i < j And i * j > 0 Then Exit Function
- If InStr(Join(arr, "&"), m & ";" & n & ";" & i & ";" & j & ";" & -LR) > 1 Then ifwr = -1: Exit Function
- If InStr(Join(brr, "&"), m & ";" & n & ";" & i & ";" & j & ";" & -LR) > 1 Then Exit Function
- LR = LR * -1
- arr(x) = m & ";" & n & ";" & i & ";" & j & ";" & LR
- f = s(m, n, i, j, x)
- End Function
复制代码 |