幻方排列

发布于 2024-12-20 20:38:48 字数 345 浏览 1 评论 0原文

我在编写递归排列函数来解决幻方问题时遇到了一些麻烦。对于这个函数,我不允许使用二维数组,只能使用列表。以下是我目前所拥有的:

def permute(size):
    magicSquare = []
    for value in permute(size**2):
        for pos in range(size**2 + 1):
            magicSquare.append(value)
    return magicSquare

size 由用户通过命令行参数定义。

如果上面编写的函数完成了排列值的任务,我有点困惑。

I'm having a little bit of trouble writing a recursive permutation function for solving Magic Squares. For this function, I'm not allowed to use two-dimensional arrays, only lists. Below is what I have currently:

def permute(size):
    magicSquare = []
    for value in permute(size**2):
        for pos in range(size**2 + 1):
            magicSquare.append(value)
    return magicSquare

size is defined by the user through command-line argument.

I'm slightly confused if the function written above accomplishes the task of permuting the values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗藏城府 2024-12-27 20:38:48

它看起来不会并且事实上基本上永远不会终止它当前的编写方式。

开始思考这个问题的一个简单方法是,幻方可以用大小为 n**2 的列表表示,因此 3x3 幻方可以用 9 长度的列表表示。由于它是一个幻方,因此您需要对值 range(1,n+1) 进行排列,例如,对于 3x3:

1 2 3
4 5 6
7 8 9

检查这是否是一个幻方(它不是“ t 因为行的总和不等于相同的值),如果是,则将其添加到你的魔方列表。不管怎样,尝试下一个排列:

1 2 3
4 5 6
7 9 8

……直到你没有排列。当然,这是一条非最佳路线,因为故障行 (1, 2, 3) 的总和仍然不会达到 15,因此有明显的优化空间,并且可以轻松丢弃不起作用的可能性。

是一个简单的工具,可以检查你的工作或为你做排列。 itertools.permutations。这将创建一个生成器,它将产生每个额外的排列,直到没有任何排列为止更多的。

请注意,对于超出小平方大小的任何内容,如果每次使用此方法尝试进行另一个递归调用,您都将超出最大递归限制。 size=3 后,您需要找到一种方法来处理这种情况。有几种方法可以处理不同复杂程度的问题,具体取决于您到底想要做什么。

It wouldn't appear to and in fact should basically never terminate the way it is currently written.

An easy way to start thinking about this problem is that a magic square can be represented by a list of size n**2, so a 3x3 magic square can be represented by a 9-length list. Since it is a magic square, you then need to permute over the values range(1,n+1), e.g., for a 3x3:

1 2 3
4 5 6
7 8 9

Check to see if this is a magic square (it isn't since the rows don't sum to the same value) and if it is, add it to your magic squares list. Either way, try the next permutation:

1 2 3
4 5 6
7 9 8

…until you are out of permutations. This is, of course, a non-optimal route because the trouble row (1, 2, 3) still won't sum to 15, so there is clear room for optimization and easily discarding possibilities that won't work.

An easy tool to either check your work or do the permutation piece for you is itertools.permutations. Which will create a generator that will yield each additional permutation until there aren't any more.

Note that for anything beyond a trivial square size you are going to exceed the maximum recursion limit if you try to make another recursive call each time using this method. You'll need to find a way to manage that situation once size=3. There are a couple of ways to handle that of varying degrees of complexity, depending on what exactly you are trying to do.

随风而去 2024-12-27 20:38:48

这是检查幻方是否存在的简单方法。

注意:请尝试使用3*3网格。

def magic():
    print "maximam 9 values"
    a=[]
    for i in range(3):
        a.append([])
        for j in range(3):
            a[i].append(input('Enter the values'))
    print a
    l1= a[0][0]+a[1][0]+a[2][0]
    l2=a[0][1]+a[1][1]+a[2][1]
    l3=a[0][2]+a[1][2]+a[2][2]
    r1=sum(a[0])
    r2=sum(a[1])
    r3=sum(a[2])
    if l1 == l2 == l3 == r1 == r2 == r3:
        print a,"Its magic square"
    else:
        print a,"not magic square"
magic()

Here is a simple method for checking magic square or not.

Note: please try by using 3*3 grid.

def magic():
    print "maximam 9 values"
    a=[]
    for i in range(3):
        a.append([])
        for j in range(3):
            a[i].append(input('Enter the values'))
    print a
    l1= a[0][0]+a[1][0]+a[2][0]
    l2=a[0][1]+a[1][1]+a[2][1]
    l3=a[0][2]+a[1][2]+a[2][2]
    r1=sum(a[0])
    r2=sum(a[1])
    r3=sum(a[2])
    if l1 == l2 == l3 == r1 == r2 == r3:
        print a,"Its magic square"
    else:
        print a,"not magic square"
magic()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文