在*解决方案中检查已经访问过的状态,以解决8个嘴式问题

发布于 2025-01-21 14:16:30 字数 592 浏览 1 评论 0原文

我正在使用A*算法进行大学作业来解决8个插头问题的解决方案,目前正在使用一种方法来识别已经访问过的拼图州以减少冗余操作的方法。我可以将这个问题发生爆发,并列出已经访问的州,并搜索正在探索的每个州,但是显然效率低下。我正在寻找某种方法来使任何难题成为唯一的整数标识符,以便我可以在布尔数组中搜索任何状态并检查它是否已访问。

我想在将数字分配给数学符号时,将数字分配给拼图的每个位置时(这样,左上方位置获得数字2,中间位置3,数字3,右上位置数字5,依此类推)。这样给定拼图的状态,

[[a, b, c]; [d, e, f]; [g, h, i]]

它的标识符将由id =(2^a)(3^b)(5^c)给出,但是很快就花了很长时间意识到这些数字变得非常非常大,并且从内存的角度来看,将大小的数组非常不切实际(我什至不认为具有数组的必要大小甚至是可能的)。

另一个选项是一个具有布尔值的8维数组,因此您可以使用拼图的相应置换值访问它,但是,在语法上,一个8维数组在审美上是非常不愉快的,而后方的疼痛只是声明变量。

有没有办法将唯一的整数标识符分配给拼图的任何给定状态,以便我可以认识到它是否已经以瞬时方式访问,而不必在已经访问过的状态的列表中搜索它?

I am working on a solution to the 8-puzzle problem using the A* algorithm for an university assignment, and currently working on a method to identify already visited states of the puzzle to reduce redundant operations. I could brute-force the issue and make a list of already visited states and search it for every state that is being explored, however that is very obviously inefficient. I am looking for some way to give any state of the puzzle an unique integer identifier so that I could search any state within a Boolean array and check whether it's been visited or not.

I thought of taking a similar approach to that of Godel's Incompleteness Theorem when assigning numbers to mathematical symbols, assigning prime numbers to each position of the puzzle (that way the top left position gets the number 2, the middle top position the number 3, the right top position the number 5 and so on). That way given a state of the puzzle

[[a, b, c]; [d, e, f]; [g, h, i]]

It's identifier would be given by Id = (2^a)(3^b)(5^c) and so on, however it doesn't take long to realize that these numbers get very, very big, and having an array that size would be extremely impractical from a memory standpoint (and I don't even think having an array the necessary size is even possible).

The other option was an 8-dimensional Array with boolean values so that you could access it with the corresponding values of the permutation of the puzzle, however an 8-dimensional array is very aesthetically unpleasing in terms of syntax and a pain in the rear just to declare the variable.

Is there a way that I can assign an unique integer identifier to any given state of the puzzle so that I could recognize if it's already been visited in an instantaneous manner instead of having to search for it in a list of already visited states?

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

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

发布评论

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

评论(1

怪我闹别瞎闹 2025-01-28 14:16:31

您需要一个 hash table ,提供O(1)搜索复杂性。

至于唯一的标识符,您可以使用某种哈希,例如:

id = state[0][0]
id = id * 37 + state[0][1]
id = id * 37 + state[0][2]
// and so on for every number in state

对于3x3板,足以代表每个州具有独特整数值的状态,这将在哈希表中效果很好。

You need a hash table, which offers O(1) search complexity.

As for a unique identifier, you can use some sort of hash, e.g.:

id = state[0][0]
id = id * 37 + state[0][1]
id = id * 37 + state[0][2]
// and so on for every number in state

For 3x3 board it would be sufficient to represent every state with unique integer value, which will play nicely with a hash table.

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