小编Ale*_*sei的帖子

在表格内生成随机模式的最佳方法是什么

我有一张桌子(2d阵列),cx r.需要在其内部生成连接单元的随机模式.没有自我交叉,也没有对角线移动.例如,参见相关图片.恩.1个 с= 6,R = 7,图案被在数字所示.

我为此编写了一个函数,它工作正常,但我正在寻找硬性优化.在下面的代码中,您可以看到如果模式进入死胡同,它只是从一开始就重建自己.如果模式长度接近或等于单元的数量c*r(在该示例中为42),则效率非常低.因此,需要一些智能解决方案,例如在完成可能的移动时对称地移动整个模式,或者为函数添加一些分析,以便它永远不会陷入死胡同.同样,对于c,r和patternLength的低值,我的示例工作正常,但我正在寻找算法完美和高性能,即使在相当高的数字.

function ClassLogic:generatePattern() 
  --[[ subfunctions ]]
  --choosing next point for the pattern
  local move = function( seq )
    --getting the last sequence point
    local last = seq[#seq]

    -- checking the nearness of walls
    local 
      wallLeft,
      wallRight,
      wallUp,
      wallDown = 
      (last.c==1),
      (last.c==config.tableSize.c),
      (last.r==1),
      (last.r==config.tableSize.r)    

    -- checking the nearness of already sequenced points
    local 
      spLeft,
      spRight,
      spUp,
      spDown = 
      (utilities.indexOfTable( seq, { c = last.c - 1, r = last.r } )~=-1),
      (utilities.indexOfTable( seq, { …
Run Code Online (Sandbox Code Playgroud)

algorithm lua

5
推荐指数
1
解决办法
95
查看次数

标签 统计

algorithm ×1

lua ×1