查找排列反转的数量
我正在查看这个,因为我正在尝试制作一个十五个谜题求解器。我实在不明白它在说什么。考虑到“如果列表的排列符号是+1,则位置是可能的”,我将如何检查给定的一组数字(从0到15,存储在数组中,0是空白)是否有效。我正在使用 javascript(如果相关的话)。
I was looking at this because I'm trying to make a fifteen puzzle solver. I don't really understand what it's talking about. How would I go about checking if a given set of numbers (from 0-15, stored in an array, 0 being the blank) is valid given that "if the permutation symbol of the list is +1 the position is possible". I'm working in javascript, if its relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑以下情况:如果您解决了一个 15 谜题,并用物理方式移除并交换了一对夹板,并替换了
14
和15
块,并将其打乱。 .你能把它恢复到有效状态吗?答案是否定的。在 15 个谜题中,您可以执行的所有移动都会保留一个不变量,并且排列符号可能指的是该不变量。
根据 http://en.wikipedia.org/wiki/Fifteen_puzzle :
要计算此奇偶校验,请查看 http://en.wikipedia.org/wiki/Parity_of_a_permutation (你也可以查看 Levi-Civita Symbol,但它有点神秘),用 python 实现它,然后计算空方块从其起始位置移动的曼哈顿距离,并取这两个值之和的奇偶校验。
例如:
这里有一些示例/测试用例:
结果:
如果您的算法并不真正关心该位置是否可能(您只是这样做是为了说“无效输入!位置不可能!”您可以忽略这部分,无论如何运行数百次迭代,如果未解决则返回“不可能!”
Consider the following: If you took a solved 15-puzzle, and with a pair of plyers physically removed and swapped and replaced the
14
and15
blocks, and scrambled it... could you return it to a valid state?The answer is no. There is an invariant that is preserved by all moves you can do in a 15-puzzle, and the permutation symbol is probably referring to that invariant.
According to http://en.wikipedia.org/wiki/Fifteen_puzzle :
To calculate this parity, check out http://en.wikipedia.org/wiki/Parity_of_a_permutation (you could also check out Levi-Civita Symbol, but it's a bit arcane), implement it in python, then calculate the manhattan distance the empty square has moved from its starting position, and take the parity of the sum of both those values.
Something like:
Here are some examples / test cases:
Results:
If your algorithm doesn't really care about whether the position is possible or not (you're just doing this to say "invalid input! position not possible!" you could ignore this part, run it anyway for a few hundred iterations, and return "impossible!" if unsolved.
由于移动这些拼图之一上的棋子需要“循环”,因此不能单独进行棋子交换。考虑董事会:
您必须交换 (11) 和 (12) 来解决它。但你怎么能呢?简单地向任一方向“循环”(11、12、15、-)永远不会改变顺序。因此,我们必须涉及更多的部分,但这样做时,我们无法保留其他部分的顺序。我们尝试的任何操作都会导致另一对的顺序被交换。例如,我们可以通过涉及 (7) 和 (8) 来纠正 (11) 和 (12),但这样做时,交换 (8) 和 (-):
因此,解决难题所需的交换次数必须是偶数,否则我们就会像上面的棋盘一样留下“奇怪的人”。
因此,如果您在解算器中检测到一次交换即可解决难题的情况,您就知道该板无法解决。
Because of the "cycles" required to move pieces on one of these puzzles, piece swaps cannot be made in isolation. Consider the board:
You must swap (11) an (12) to solve it. But how can you? Simply "cycling" (11, 12, 15, -) in either direction will never change the order. Therefore we must involve more pieces, but in doing so, we cannot preserve the order of those other pieces. Anything we try will result in the order of another pair being swapped. For example, we might correct (11) and (12) by involving the (7) and (8), but in doing so, swap the (8) and (-):
Therefore, the number of swaps required to solve the puzzle must be even, or we are left with an "odd man out" as in the board above.
Therefore again, if you detect in your solver a situation in which a single swap will solve the puzzle, you know that this board cannot be solved.