解决“旋转管道游戏”的算法?

huangapple go评论64阅读模式
英文:

Solving algorithm for a "rotating pipes game"?

问题

我和我的朋友正在为我们的学校项目开发一款游戏。
目标是创建一个带有解谜功能的拼图游戏。
我们的游戏概念类似于这个视频:这是我找到的最佳视频

因此,通过一些研究,我只找到了一些用于寻路的算法(A*,Prim等),但没有找到用于改变可能性的算法。我想知道是否有人已经找到了相关的思路,还是我需要从头开始。

英文:

me and my friends are realising a game for our school project.
The goal is to create a puzzle grid game with a solver.
Our game will look like that in the idea : it's the best video I found.

So with some research I found only algorithm for a pathway (A*, Prim...) but nothing for changing possibilities and I would like to know if someone already found an idea or if I will have to go from scratch.

答案1

得分: 0

A* 和 Dijkstra 的算法都是解决这个问题的好选择,但你需要决定 要在哪个图上操作

这些算法找到了最短路径,而你需要从初始状态到解决方案的(可能是最小的)移动序列,因此你需要一个图,其中从源到目标的路径表示这样一个序列。

例如,想象一个图,其中每个可能的棋盘配置都有一个顶点,当你可以通过一次移动从一个配置到另一个配置时,两个顶点之间有一条边。现在,从初始配置到解决配置的每条路径都是一系列解决移动。

这种方法有效,但是图很大 - 你的算法将运行缓慢并且需要大量内存。

对于这个问题,最好逐行处理。如果你已经设置了前 n 行的管道,那么只有最后一行的状态会影响你如何设置其余的行。最后一行的“状态”是关于哪些单元格连接到空气、哪些连接到水以及哪些形成密封连接的信息。如果你的谜题只有 6 条管道,那么每一行可能只有大约 100 个可访问状态。

因此,想象一个图,其中你有所有每一行可访问状态的顶点。第 i 行的状态与第 i+1 行的状态相连,如果在给定第 i 行的状态的情况下,有一种方法可以定向第 i+1 行的管道以产生该状态。每条边可以有一个权重,表示你必须在第 i+1 行中进行多少次移动才能到达那里。

现在,一个解决移动序列由从固定起点到最后一行的解决状态的路径表示。同样,你可以应用 Dijkstra 或 A* 来找到这条路径,这次图的大小是合理的。

请注意,在任何情况下,你实际上都不需要构建这个图。你只是想象它存在,并编写你的算法以使用谜题的紧凑表示方法来遍历它。这被称为在隐式图上操作,这通常是在实践中使用简单图算法的方式。

英文:

A* and Dijkstra's algorithm are both good choices for solving this problem, but you need to decide what graph to operate on.

These algorithms find a shortest path, and you want a (probably minimal) sequence of moves from the initial state to a solution, so you need graph in which a path from source to target represents such a sequence.

For instance, imagine a graph with a vertex for every possible board configuration, and an edge between any two vertices when you can get from one configuration to the other by one move. Now every path from the initial configuration to a solving configuration is a solving sequence of moves.

This works, but it is a very big graph -- your algorithm will run slowly and take a lot of memory.

For this problem, it's better to work row by row. If you've already set the pipes in the first n rows, then only the state of the last row affects how you have to set the rest. The "state" of the last row is the information about which cells connect to air, which connect to water, and which form a sealed connection to each other. If your puzzle is only 6 pipes across, then there will probably be only about 100 accessible states for every row.

So imagine a graph where you have vertices for all the accessible states of each row. A state in row i connects to a state in row i+1 if, given the state of row i, there's a way to orient the pipes in row i+1 to produce that state. Each edge can have a weight indicating the number of moves you have to make in row i+1 to get there.

Now a solving sequence of moves is represented by a path from the fixed start to a solving state in the last row. Again you can apply Dijkstra's or A* to find this path, and this time the graph is a reasonable size.

Note that in no case do you actually need to construct this graph. You just imagine that it exists and write your algorithm to traverse it using a compact representation of the puzzle. This is called operating on an implicit graph, and that's usually how simple graph algorithms are used in practice.

huangapple
  • 本文由 发表于 2023年5月17日 16:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定