'魔术广场'算法

时间:2014-10-20 00:51:07

标签: arrays algorithm lua lua-table

作为一项实验,我正在尝试创建一个魔术方块程序,用九个数字检查每个可能的方块。对于那些不知道的人来说,魔方是一个3x3的数字1-9网格,其中每行,每列和对角线加起来为15.例如:

http://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Magicsquareexample.svg/180px-Magicsquareexample.svg.png

我如何使用带Lua的桌子检查每个方格?我从下表开始:

local sq = {
    1, 1, 1,
    1, 1, 1
    1, 1, 1
}

我如何以正确的顺序检查每张桌子?我能够在纸上画出我的想法,但我不完全确定如何将其翻译成代码。我已经创建了函数来检查方块是否是“魔术”(下面),但我不确定如何以正确的方式增加每个数字。

local isMagic = function(s)
    return (
        s[1] + s[2] + s[3] == 15 and
        s[4] + s[5] + s[6] == 15 and
        s[7] + s[8] + s[9] == 15 and
        s[1] + s[4] + s[7] == 15 and
        s[2] + s[5] + s[8] == 15 and
        s[3] + s[6] + s[9] == 15 and
        s[1] + s[5] + s[9] == 15 and
        s[3] + s[5] + s[7] == 15
    )
end 

2 个答案:

答案 0 :(得分:1)

根据我在这里看到的内容,有三种模式:

1) if we can define step by 3, we compare columns: 
sum(tab[x] for x in range(step)) == sum(tab[x] for x in xrange(step+1,step*2))== sum(tab[x] for x in xrange(2*step+1,step*3))

2) raws:
sum(tab[x] for x in range(step*step) if x%step==0) == sum(tab[x] for x in range(step*step) if x%step==1)== sum(tab[x] for x in range(step*step) if x%step==2) ===> till we x%step== step-1

3) Diagonales: 
sum(tab[x] for x in range(step*step) if x%(step+1)==0) == sum(tab[x] for x in range(step*step) if x%(step+1)==step-1) 

答案 1 :(得分:0)

首先,你有一套2D套装,你为什么要使用一维清单? 我更喜欢你的广场像

square[1-3][1-3] 

所以你可以检查X处的每一行和Y处的每一行,然后检查2对角线。

而不是

square[1-9]

您必须对此解决方案中的检查进行硬编码,否则您将无法在不编码新内容的情况下设置其他方形尺寸。

就像:

local square = {}
square[1] = {[1] = 2, [2] = 7, [3] = 6}
square[2] = {[1] = 9, [2] = 5, [3] = 1}
square[3] = {[1] = 4, [2] = 3, [3] = 8}

这是我的代码,用于检查方块是否是魔法。 您可以根据需要设置大小。

#!/usr/local/bin/lua

    function checkTheSquare(square, check_for)
        local dia_sum     = 0   -- we will add the diagonal(u.l. to l.r.) values here
        local inv_dia_sum = 0   -- we will add the diagonal(u.r. to l.l.) values here

      for i = 1, #square do     -- for every [i] line in square
        local temp_sum    = 0   -- just holds the values for the [i] line temporary

        for j = 1, #square do   -- for every [j] line in square
          temp_sum = temp_sum + square[i][j]   -- add the square[i][j] value to temp
        end

        dia_sum = dia_sum + square[i][i]  
        -- this will go like: [1][1] -> [2][2] -> [3][3] ...

        inv_dia_sum = inv_dia_sum + square[i][#square-i+1]
        -- this will go like: [1][3] -> [2][2] -> [3][1] ...            

        if temp_sum ~= check_for then return false end
        -- first possible find of wrong line -> NOT MAGIC

      end

      if dia_sum ~= check_for and inv_dia_sum ~= check_for then
        return false 
        -- due its not magic
      end

      return true
      -- ITS MAGIC! JUHUUU
    end


    local square = {}
    square[1] = {[1] = 16, [2] = 3,  [3] = 2,  [4] = 13}
    square[2] = {[1] = 5,  [2] = 10, [3] = 11, [4] = 8}
    square[3] = {[1] = 9,  [2] = 6,  [3] = 7,  [4] = 12}
    square[4] = {[1] = 4,  [2] = 15, [3] = 14, [4] = 1}

    local check_for = 34

    print(checkTheSquare(square, check_for) == true)

与您自己的代码相比,它看起来更复杂但是:

  1. 你不是一个算法,它是一个程序。
  2. 我很有活力。人们还可以在方形字段中添加随机值,将平均所需的平均次数,以提供一个魔方。