ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
123
返回列表 发新帖
楼主: yynrzwh

[讨论] agstick版主的捡金币问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-3 17:03 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Option Explicit

  2. Dim map() As Long
  3. Dim path() As Variant


  4. Private Sub cmdCount_Click()
  5. Dim n&, x&, y&, x1&, y1&, k&, p&

  6.    n = Range("m2")
  7.   
  8.   Range("p4") = count(n, map, path)
  9.   
  10.    Cells(n, n).Interior.Color = RGB(225, 0, 0) Xor RGB(0, 155, 21)
  11.    x = n - 1
  12.    x1 = n - 1
  13.   For k = 2 * n - 2 To 1 Step -1
  14.      p = path(k)(x)(x1)
  15.      
  16.      If (p = 2) Or (p = 3) Then x = x - 1
  17.      y = k - 1 - x
  18.      Cells(y + 1, x + 1).Interior.Color = RGB(225, 0, 0)
  19.      If (p = 1) Or (p = 3) Then x1 = x1 - 1
  20.      y1 = k - 1 - x1
  21.       Cells(y1 + 1, x1 + 1).Interior.Color = RGB(0, 155, 21) Xor Cells(y1 + 1, x1 + 1).Interior.Color
  22.   Next
  23. End Sub

  24. Private Sub cmdStat_Click()

  25. Dim n&, num&, max&, i&, j&

  26. Range("a1:j10").ClearContents
  27. Range("a1:j10").Interior.Pattern = xlPatternNone

  28. n = Range("m2")
  29. num = Range("n2")
  30. max = Range("o2")
  31. If n < 1 Or n > 15 Then MsgBox "格子个数超出范围": Exit Sub
  32. If num > n * n Then MsgBox "个数超出范围": Exit Sub
  33. createMap n, num, max, map
  34. For i = 0 To n - 1
  35. For j = 0 To n - 1
  36.     If map(i, j) > 0 Then Cells(j + 1, i + 1) = map(i, j)
  37. Next
  38. Next
  39. End Sub
  40. Sub createMap(n As Long, num As Long, max As Long, map() As Long)

  41. Dim temp&(), i&, r&, s&, last&, m&

  42. last = n * n - 1
  43. ReDim temp(0 To last)
  44. ReDim map(0 To n, 0 To n)

  45. For i = 0 To last
  46.     temp(i) = i
  47. Next

  48. VBA.Randomize
  49. For i = 0 To num - 1
  50.     s = Int(max * Rnd + 1)
  51.     r = Int(Rnd * (last - i))
  52.     m = temp(r)
  53.     map(m Mod n, m \ n) = s
  54.     temp(r) = temp(last - i)
  55. Next

  56. End Sub

  57. Function count(n As Long, map() As Long, path() As Variant) As Long
  58.    
  59.    Dim k&, x&, y&, x1&, y1&, xs&, xe&, last&, cur&, before&
  60.    Dim p() As Variant
  61.    Dim p1() As Byte
  62.    Dim state() As Long
  63.    
  64.    last = n * 2 - 2
  65.    ReDim path(0 To last)
  66.    ReDim state(1, n - 1, n - 1)
  67.    state(0, 0, 0) = map(0, 0)
  68.    before = 0
  69.    
  70.    For k = 1 To last
  71.      If k < n Then
  72.         xs = 0
  73.         xe = k
  74.      Else
  75.         xs = k - (n - 1)
  76.         xe = n - 1
  77.      End If
  78.      ReDim p(xs To xe)
  79.      
  80.      Dim s1&, s&, s0&, sMax&
  81.      
  82.      cur = before Xor 1
  83.      
  84.      For x = xs To xe
  85.         ReDim p1(x To xe)
  86.         y = k - x
  87.         s = map(x, y)
  88.         
  89.         For x1 = x To xe
  90.               
  91.         sMax = 0
  92.         y1 = k - x1
  93.         s1 = map(x1, y1)
  94.         If y > 0 Then
  95.             If x1 > 0 And x <> x1 Then
  96.                 s0 = state(before, x, x1 - 1) + s + s1
  97.                 If s0 >= sMax Then
  98.                      sMax = s0
  99.                      p1(x1) = 1
  100.                 End If
  101.             End If
  102.             If y1 > 0 Then
  103.                 s0 = state(before, x, x1) + s
  104.                 If x <> x1 Then s0 = s0 + s1
  105.                 If s0 >= sMax Then
  106.                    sMax = s0
  107.                     p1(x1) = 0
  108.                 End If
  109.             End If
  110.         End If
  111.         If x > 0 Then
  112.             If x1 > 0 Then
  113.                 s0 = state(before, x - 1, x1 - 1) + s
  114.                 If x <> x1 Then s0 = s0 + s1
  115.                 If s0 >= sMax Then
  116.                    sMax = s0
  117.                     p1(x1) = 3
  118.                 End If
  119.             End If
  120.             If y1 > 0 Then
  121.                 s0 = state(before, x - 1, x1) + s
  122.                 If x <> x1 Then s0 = s0 + s1
  123.                 If s0 >= sMax Then
  124.                    sMax = s0
  125.                    p1(x1) = 2
  126.                 End If
  127.             End If
  128.          End If
  129.          state(cur, x, x1) = sMax
  130.         
  131.      Next
  132.      p(x) = p1
  133.     Next
  134.    
  135.     path(k) = p
  136.     before = cur
  137.   Next
  138.   
  139.   count = state(before, x - 1, x1 - 1)
  140.   
  141. End Function




复制代码


捡金币2.rar

89.95 KB, 下载次数: 2

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-20 08:48 , Processed in 0.037898 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表