最简单的魔方编码算法?

发布于 2024-08-03 13:39:24 字数 51 浏览 5 评论 0 原文

用 Java 编写解决魔方问题的相对简单的算法是什么?效率也很重要,但属于次要考虑因素。

What would be a relatively easy algorithm to code in Java for solving a Rubik's cube. Efficiency is also important but a secondary consideration.

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

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

发布评论

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

评论(7

妖妓 2024-08-10 13:39:24

执行随机操作,直到获得正确的解决方案。最简单的算法,效率最低。

Perform random operations until you get the right solution. The easiest algorithm and the least efficient.

楠木可依 2024-08-10 13:39:24

我发现的最简单的算法是这样的:

http://www.chessandpoker.com/rubiks-cube-solution.html

编写代码看起来并不难。 Yannick M.的回答中提到的链接看起来也不错,但是 ' 的解决方案cross' 步骤对我来说可能有点复杂。

有许多开源求解器实现可供您查看。这是一个 Python 实现。这个 Java applet 还包含一个求解器,并且源代码可用。还有一个 Javascript 求解器,还带有可下载的源代码。

安东尼·加特林的回答对这口井提出了一个很好的观点-Prolog 对该任务的适用性。这是一篇关于如何编写自己的 Prolog 求解器 的详细文章。它使用的启发式方法特别有趣。

The simplest non-trivial algorithm I've found is this one:

http://www.chessandpoker.com/rubiks-cube-solution.html

It doesn't look too hard to code up. The link mentioned in Yannick M.'s answer looks good too, but the solution of 'the cross' step looks like it might be a little more complex to me.

There are a number of open source solver implementations which you might like to take a look at. Here's a Python implementation. This Java applet also includes a solver, and the source code is available. There's also a Javascript solver, also with downloadable source code.

Anthony Gatlin's answer makes an excellent point about the well-suitedness of Prolog for this task. Here's a detailed article about how to write your own Prolog solver. The heuristics it uses are particularly interesting.

爱情眠于流年 2024-08-10 13:39:24

可能想查看:http://peter.stillhq.com/jasmine/rubikscubesolution.html< /a>

具有解决 3x3x3 魔方算法的图形表示

Might want to check out: http://peter.stillhq.com/jasmine/rubikscubesolution.html

Has a graphical representation of an algorithm to solve a 3x3x3 Rubik's cube

挽心 2024-08-10 13:39:24

我知道你的问题与 Java 有关,但实际上,像 Prolog 这样的语言更适合解决魔方之类的问题。我认为这可能是为了一堂课,你可能没有选择工具的余地。

I understand your question is related to Java, but on a practical note, languages like Prolog are much better suited problems like solving a Rubik's cube. I assume this is probably for a class though and you may have no leeway as to the choice of tool.

最佳男配角 2024-08-10 13:39:24

作为参考,你当然可以看看这个java实现。 -->
使用两阶段算法求解魔方。并尝试过这段代码,它也有效。

For your reference, you can certainly look at this java implementation. -->
Uses two phase algorithm to solve rubik's cube. And have tried this code and it works as well.

勿忘心安 2024-08-10 13:39:24

你可以通过BFS(广度优先搜索)来做到这一点。我认为实现并不难(它是图类别下最简单的算法之一)。通过使用称为队列的数据结构来完成此操作,您真正要做的是构建 BFS 树并找到从给定条件到所需条件的所谓最短路径。该算法的缺点是效率不够(在没有任何修改的情况下,即使求解 2x2x2 立方体所需的时间也约为 5 分钟)。但你总能找到一些技巧来提高速度。

老实说,这是名为“算法简介”来自麻省理工学院。这是作业的链接:http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/assignments/MIT6_006F11_ps6.pdf 。他们有一些库可以帮助您将其可视化并帮助您避免不必要的工作。

You can do it by doing BFS(Breadth-First-Search). I think the implementation is not that hard( It is one of the simplest algorithm under the category of the graph). By doing it with the data structure called queue, what you will really work on is to build a BFS tree and to find a so called shortest path from the given condition to the desire condition. The drawback of this algorithm is that it is not efficient enough( Without any modification, even to solver a 2x2x2 cubic the amount time needed is ~5 minutes). But you can always find some tricks to boost the speed.

To be honest, it is one of the homework of the course called "Introduction of Algorithm" from MIT. Here is the homework's link: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/assignments/MIT6_006F11_ps6.pdf. They have a few libraries to help you to visualize it and to help you avoid unnecessary effort.

静谧幽蓝 2024-08-10 13:39:24

我想一种解决方案是同时运行所有可能的路线。这听起来确实很愚蠢,但逻辑是这样的——超过 99% 的可能的争夺将在 20 步以内解决。这意味着,尽管你循环了无数种可能性,但你最终还是会这么做。本质上,这可以通过将你的第一步作为打乱的立方体来实现。然后,您将在第一个立方体上的每个可能的移动中将新的立方体存储在变量中。对于每个新立方体,您都执行相同的操作。在每次可能的移动之后检查它是否完成,如果是,那么这就是解决方案。为了确保您拥有解决方案,您需要在每个魔方上提供额外的数据,说明达到该阶段所完成的动作。

One solution is to I guess simultaneously run all possible routes. That does sound stupid but here's the logic - over 99% of possible scrambles will be solvable in under 20 moves. This means that although you cycle through huge numbers of possibilities you are still going to do it eventually. Essentially this would work by having your first step as the scrambled cube. Then you would have new cubes stored in variables for each possible move on that first cube. For each of these new cubes you do the same thing. After each possible move check if it is complete and if so then that is the solution. Here to make sure you have the solution you would need an extra bit of data on each Rubiks cube saying the moves done to get to that stage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文