【问题标题】:Check if hex is in range of hexagonal tiling with weighted hexes检查十六进制是否在加权六边形的六边形平铺范围内
【发布时间】:2018-12-22 23:02:31
【问题描述】:

问题

假设我有一个(无限的)六边形平铺。从 1 个六边形开始,我可以移动到除了一些“空心”六边形之外的每个相邻的。一些六边形的权重为 1,而其他六边形的权重为 2。最大权重为 x,如何从x 下方的起始十六进制中获取所有具有累积权重的六边形?

上下文

我正在尝试为游戏 Civilization V 制作一个模组。在其中,我需要获得一个单位在 10 回合内可以到达的所有图块的集合,知道这个单位每回合有 1 个移动点,并且除了道路以外的每块瓷砖都要花费 1 MP 才能到达(道路花费 0.5)。山区和海洋瓷砖是不可接近的。 简而言之,它是所选单位周围显示区域的扩展版本,显示单位 1 圈距离内的所有图块。

当前测试

截至目前,我已经尝试了 2 种解决方案,但似乎没有一个非常有效。我的大多数尝试都不知道要检查哪些图块(因为它们尚未检查,或者因为尚未检查它们的最衬衫路径)以及不检查哪些图块,最终多次检查范围内的每个图块,并且拒绝几个看似在范围内但已从比必要更长的路径检查的图块,因此认为它们太远了。

  • 第一个非常幼稚,对每个图块进行递归检查 围绕起始图块,消除所有累积的图块 重量超过x
  • 对于第二个解决方案,我保持不变 结构,但我添加了一个图块不应该的条件过滤 如果与原点的距离低于累积值,则被拒绝 计算到那里的权重(因为具有不同累积的多条路径 权重可以导致相同的瓷砖)。但是有很多这样的情况 断言是错误的。

我真的需要一些关于如何做到这一点的建议。

谢谢你, 元

【问题讨论】:

    标签: algorithm lua


    【解决方案1】:

    您应该使用 Dijkstra 算法来找到到附近图块的最短路径。由于 Dijkstra 的算法是按照长度递增的顺序找到最短路径的,所以当您找到比x 更长的最短路径时,您可以停止。

    https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

    【讨论】:

      【解决方案2】:

      这里不需要 Dijkstra。
      简单的“波浪”算法就足够了。

      您需要一个数组 dist 来存储场地上每个六边形的数字(距离)。
      您还需要另一个数组wave 来存储最近刷新的六边形列表。

      伪代码:

      variable x = 10 -- distance in MP
      loop for each hexagon on the field
          dist[hexagon] = +infinity
      end-loop
      dist[unit_hexagon] = 0
      wave = empty array
      append unit_hexagon to array wave
      loop while array wave is not empty
          create new empty array new_wave
          loop for each hexagon in array wave
              loop for each of 6 adjacent hexagons of hexagon
                  if adjacent_hexagon is accessible (not a mountain)
                      variable adj_dist = dist[hexagon] + price(adjacent_hexagon)
                      -- where price = 0.5 for roads, 1 for other cells
                      if (adj_dist < dist[adjacent_hexagon]) and (adj_dist < x) then
                          dist[adjacent_hexagon] = adj_dist
                          append adjacent_hexagon to array new_wave
                      end-if
                  end-if
              end-loop
          end-loop
          wave = empty array
          copy everything from array new_wave to array wave
      end-loop
      loop for each hexagon in the field
          if dist[hexagon] < +infinity then
              the hexagon is inside colored area around the unit
          end-if
      end-loop
      

      【讨论】:

      • 非常感谢!我不知道这种波浪算法。由于 Civ V API,我在实现它时遇到了一些麻烦,但现在它可以工作了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多