如何解决5 * 5立方的高效简便的方法高效、立方、如何解决、简便

2023-09-11 03:33:36 作者:唯独有你

有5 * 5名为幸福魔方问题立方体谜,其中对于给定的垫子上,需要做出一个立方体。 http://www.mathematische-basteleien.de/cube_its.htm#top

There is 5*5 cube puzzle named Happy cube Problem where for given mat , need to make a cube . http://www.mathematische-basteleien.de/cube_its.htm#top

这就像,6蓝色垫是下列人士提供

Its like, 6 blue mats are given-

从下面的垫子,需要得出一个立方体 -

From the following mats, Need to derive a Cube -

它有3个解决方案,这些方式。 因此,像第一个幼崽

These way it has 3 more solutions. So like first cub

对于这样的问题,我能想象到的最简单的方法是递归的基础,其中的每一个立方体,我有6个位置,每个位置我会尝试检查所有其他队友,哪些适合,我会再去递归解决同样的。比如寻找每一个立方体的所有排列,然后找到其适合best.So动态规划方法。

For such problem, the easiest approach I could imagine was Recursion based where for each cube, I have 6 position , and for each position I will try check all other mate and which fit, I will go again recursively to solve the same. Like finding all permutations of each of the cube and then find which fits the best.So Dynamic Programming approach.

但我会犯错的递归的负荷,那么,有没有更好的简单的方法,我可以用它来解决一样吗?

But I am making loads of mistake in recursion , so is there any better easy approach which I can use to solve the same?

我做出来的矩阵提供的每个垫子或图表的,那么我旋转他们每90钟明智的4倍,anticlock明智次。我翻转阵列,也做了同样的,现在每个上述迭代我将不得不重复其他多维数据集的步骤,如此反复递归。

I made matrix out of each mat or diagram provided, then I rotated them in each 90 clock-wise 4 times and anticlock wise times . I flip the array and did the same, now for each of the above iteration I will have to repeat the step for other cube, so again recursion .

 0 0 1 0 1
 1 1 1 1 1
 0 1 1 1 0
 1 1 1 1 1
 0 1 0 1 1
-------------
 0 1 0 1 0
 1 1 1 1 0
 0 1 1 1 1
 1 1 1 1 0
 1 1 0 1 1
-------------
 1 1 0 1 1
 0 1 1 1 1
 1 1 1 1 0
 0 1 1 1 1
 0 1 0 1 0
-------------
 1 0 1 0 0
 1 1 1 1 1
 0 1 1 1 0
 1 1 1 1 1
 1 1 0 1 0
-------------

1st - block is the Diagram
2nd - rotate clock wise
3rd - rotate anti clockwise
4th - flip

还在苦苦逻辑梳理。

Still struggling to sort out the logic .

推荐答案

我不能相信这一点,但实际上我写了一组脚本早在2009年,以暴力破解的解决方案,这个确切的问题,对于简单的立方体情况。我只是把code Github上: https://github.com/niklasb/3d-puzzle

I can't believe this, but I actually wrote a set of scripts back in 2009 to brute-force solutions to this exact problem, for the simple cube case. I just put the code on Github: https://github.com/niklasb/3d-puzzle

不幸的是,文件是在德国,因为这是唯一的语言,我的团队明白,但源$ C ​​$ C评论都是英文的。特别是,检查出文件 puzzle_lib.rb

Unfortunately the documentation is in German because that's the only language my team understood, but source code comments are in English. In particular, check out the file puzzle_lib.rb.

这种方法确实只是一个简单的回溯算法,我认为这是要走的路。我真的不能说这是的轻松的不过,据我记得3-D方面是一个挑战。我实现的一个优化:找到所有的对称性事前只尝试了一块每一个独特的定位。我们的想法是,更多的特点的件件都是,放置件少的选项存在,所以我们可以提前修剪。在许多对称的情况下,可能存在大量的可能性,我们希望检验仅那些是唯一的,最多对称

The approach is indeed just a straightforward backtracking algorithm, which I think is the way to go. I can't really say it's easy though, as far as I remember the 3-d aspect is a bit challenging. I implemented one optimization: Find all symmetries beforehand and only try each unique orientation of a piece. The idea is that the more characteristic the pieces are, the less options for placing pieces exist, so we can prune early. In the case of many symmetries, there might be lots of possibilities and we want to inspect only the ones that are unique up to symmetry.

基本上算法的工作原理如下:首先,分配一个固定的顺序,以立方体的两侧,让我们把它们编号为0至5的例子。然后执行下面的算法:

Basically the algorithm works as follows: First, assign a fixed order to the sides of the cube, let's number them 0 to 5 for example. Then execute the following algorithm:

def check_slots():
    for each edge e:
        if slot adjacent to e are filled:
            if the 1-0 patterns of the piece edges (excluding the corners)
                   have XOR != 0:
                return false
            if the corners are not "consistent":
                return false
    return true

def backtrack(slot_idx, pieces_left):
    if slot_idx == 6:
        # finished, we found a solution, output it or whatever
        return
    for each piece in pieces_left:
        for each orientation o of piece:
            fill slot slot_idx with piece in orientation o
            if check_slots():
                backtrack(slot_idx + 1, pieces_left \ {piece})
            empty slot slot_idx

角落一致性是有点棘手:无论是拐角处都经过相邻块只有一个被填充,或者必须是一个尚未空闲插槽存取,即不是由已分配部分切断

The corner consistency is a bit tricky: Either the corner must be filled by exactly one of the adjacent pieces or it must be accessible from a yet unfilled slot, i.e. not cut off by the already assigned pieces.

当然,你可以忽略放弃一些一致性检查的一部分或全部,只签到底,看到只有8 ^ 6 * 6!可能的配置整体。如果你有超过6片,它早修剪变得更为重要。

Of course you can ignore to drop some or all of the consistency checks and only check in the end, seeing as there are only 8^6 * 6! possible configurations overall. If you have more than 6 pieces, it becomes more important to prune early.