我在设计用于解决迷宫问题的算法时遇到问题。
我使用了这里的算法。http://www.cs.bu.edu/ education/alg/maze/
FIND-PATH(x, y)
- if (x,y 在迷宫外) return false
- if (x,y 是目标) return true
- if (x,y not open) return false
- 标记 x ,y 作为解决方案路径的一部分
- if (FIND-PATH(x,y 的北边) == true) 返回 true
- if (FIND-PATH(x,y 的东边) == true) 返回 true
- if (FIND-PATH(South of x,y) == true) return true
- if (FIND-PATH(West of x,y) == true) return true
- 取消标记 x,y 作为解决方案路径的一部分
- 返回错误
这是一个递归解决方案,我对其进行了修改,使其即使在找到退出后仍会继续,以便它也可以找到其他解决方案。 它似乎有效,只是它似乎找到了我所知道的可能解决方案总数的一半。
- if (x,y is goal) return true 更改为 return false。
有人知道这种算法可能存在什么问题,导致可能的解决方案总数只有一半吗? 我也遇到了查找死胡同路径总数的问题,对此有什么建议吗?
I am having a problem with a algorithm that is designed to solve mazes.
I used an algorithm from here.http://www.cs.bu.edu/teaching/alg/maze/
FIND-PATH(x, y)
- if (x,y outside maze) return false
- if (x,y is goal) return true
- if (x,y not open) return false
- mark x,y as part of solution path
- if (FIND-PATH(North of x,y) == true) return true
- if (FIND-PATH(East of x,y) == true) return true
- if (FIND-PATH(South of x,y) == true) return true
- if (FIND-PATH(West of x,y) == true) return true
- unmark x,y as part of solution path
- return false
It is a recursive solution , i modified it such that it will continue even after finding exit so that it can find other solutions as well. It seems to work , just that it seems to find half the total number of solutions that i know are possible.
- if (x,y is goal) return true is changed to return false.
Anyone know what might be the problem with such an algorithm resulting in half the number of total possible solutions? I also have a problem into finding the total number of dead end paths, any suggestions on that?
发布评论
评论(5)
这是一个示例迷宫
This is a sample maze
对于死胡同的数量,您需要这样的东西:
3.5 if (x,y 的北边不开放) 和 (x,y 的南边不开放) 和 (x,y 的西边不开放) 和 (x 的东边, y 未打开)死胡同++
For the number of dead ends, you need something like that:
3.5 if (North of x,y not open) and (South of x,y not open) and (West of x,y not open) and (East of x,y not open) deadends++
我尝试用java简单地实现该算法。 我的结论是,您描述的算法有效,即使对于查找多条路径也是如此。 或者,您可能想出了一个比我更聪明的测试用例。 (请发布你的迷宫,以便我可以尝试我的算法)
我的死胡同计数器的实现可能不是最有效的,但它完成了工作。 对于每个当前访问的 OPEN 节点,它会检查 4 个周围节点:
这是我编写的 java 代码(注意!相当长)。 如果您愿意,另一种方法是将路径存储在堆栈上,每次将节点设置为“VISITED”时压入一个节点,每次将其设置回“OPEN”时弹出一个节点。 每次达到目标时,应复制并保存保存当前路径的堆栈。
如果删除标记为“死胡同调查步骤”的 if 块,则此实现几乎与问题中描述的完全相同。
上面的算法报告了示例迷宫的四个不同的解决方案路径和两个死胡同,该迷宫被硬编码到代码中。
<编辑>
您获得的解决方案路径太少的原因可能是对解决方案路径的误解吗? 例如,考虑这个迷宫:
这个迷宫只有一条有效的解决方案路径。 这是因为您只能访问每个节点一次,因此绕圆形路径不是有效的解决方案路径,因为这将访问 S 以东和 E 以北的节点两次。 您使用的算法隐含了解决方案路径的定义。
如果允许多次访问同一个节点,就会有无限多个解决方案,因为您可以绕圈 1、2、3 ... 无限多次。
正如您所说,每次将节点设置为“VISITED”时,我都会增加路径长度,并在每次设置时减少路径长度将已访问的节点恢复为打开状态。
为了记录最短路径长度,我还有一个 ShortestPath int 值,我将其初始化为 Integer.MAX_VALUE。 然后,每次达到目标时,我都会这样做:
至于死胡同……我尝试用手数它们,我认为 9 似乎是正确的。 这是 Sareen 发布的迷宫,但标记了死胡同(由手)与 D:
你还能找到更多吗?
还是我误解了你所说的死胡同的意思?
我认为死胡同意味着:一个只能从一个方向到达的节点。
示例 1:
上面的迷宫有一个死胡同。
例2:
上面的迷宫没有死胡同。 即使您位于最北边的可访问节点之一,仍然有两个相邻的非 WALL 方块。
您对死胡同还有其他定义吗?
I tried making a simple-minded implementation of the algorithm in java. My conlusion was that the algorithm you described works, even for finding multiple paths. Or, possibly, you managed to think of a more clever test case than me. (Please post your maze so I can try my algorithm on it)
My implementation of the dead end counter is probably not the most efficient one, but it gets the job done. For each current OPEN node that is visited, it checks the 4 surrounding nodes:
This is the java code I wrote (beware! pretty long). An alternative would be, if you wish, to store the path on a stack, pushing a node each time it is set to VISITED and popping a node each time it is set back to OPEN. Each time the GOAL is reached, the stack holding the current path should be copied and saved.
If the if block marked with a comment as "dead-end-investigation-step" is removed, this implementation is almost exactly equal to the one described in the question.
The algorithm above reports four different solution paths and two dead ends to the example maze which is hardcoded into the code.
<EDIT>
Might the reason for why you get too few solution paths be a misinterpretation of what is a solution path? For example, consider this maze:
This maze has only one valid solution path. This is because you are only allowed to visit each node once, so going around the circular path is not a valid solution path since that would visit the node east of S and north of E twice. This definition of a solution path is implied by the algorithm that you use.
If one were to allow visiting the same node multiple times, there would be infinitely many solutions, as you could go around the circle 1, 2, 3 ... to infinitely many times.
</EDIT>
<EDIT2>
Exactly as you say, I increase the pathLength each time I set a node to VISITED, and decrease the path length each time I set a VISITED node back to OPEN.
To record the shortest path length I also have a shortestPath int value which I initiate to Integer.MAX_VALUE. Then, each time I have reached the goal, I do this:
As for the dead ends... I tried counting them by hand and I thought 9 seemed right. Here is the maze posted by Sareen, but with the dead ends marked (by hand) with a D:
Can you find any more?
Or did I misinterpret what you meant by dead end?
I thought dead end means: A node to which you can only come from one direction.
Example 1:
The maze above has one dead end.
Example 2:
The above maze has no dead ends. Even if you are at one of the accessible nodes furthest to the north, there are still two adjacent non-WALL squares.
Do you have another definition of dead end?
</EDIT2>
您不需要尝试找到一种穿过迷宫的方法,而是需要找到(并因此绘制)多种穿过迷宫的方法。
为此,您需要标记您去过的位置(在特定路径上)。 如果您到达已经去过的地点,则需要将该路线标记为死胡同。
递归函数仍然是可行的方法,但请确保通过递归函数传递 (placesIhaveBeen) 结构。
当到达 N、S、E、W 都被阻塞的点时,递归循环需要中断。 (你以前来过那里,你不能朝那个方向走,它在迷宫外面)
当您达到目标时,递归循环也需要中断。
如果达到目标 - 将全局变量增加一。
如果你无处可去——将你的死胡同增加一个。
我无法为此编写 pcode(这会花费太长时间),但我相信秘密就在一个函数中,如果 N、S、E 和 W 都被阻止,该函数将返回 true。
补充我最初的答案
关于为什么我将我去过的区域视为“封锁”,以及为什么我将它们视为死胡同......
我将上述迷宫部分分类为死胡同,如果不将我去过的地方视为封锁区域,我不知道如何识别它。
我意识到这会导致以下内容也出现死胡同,但我看不到解决方法。
Rather than trying to find one way through the maze, you need to find (and therefore map) multiple ways through the maze.
In order to do this, you need to mark where you've been (on a particular path). If you reach a point you've already been to, you need to flag that route as a dead end.
A recursive function is still the way to go, but make sure that you pass the (placesIhaveBeen) structure through the recursive function.
The recursive loop needs to break when you get to a point where N,S,E,W are all blocked. (You've been there before, you can't go in that direction, it's outside the maze)
The recursive loop also needs to break when you reach your target.
If you get to your target - increase a Global Variable by one.
If you've nowhere to go - increase your dead-ends by one.
I can't write the pcode for this (it'll take too long), but I believe that the secret is in a function that returns true if N, S, E and W are all blocked.
Addition to my initial answer
With regard to why I treat areas I've been to as "blocked", and why I treat them as dead ends....
I would classify the above maze part as a dead end, and I can't see how I can identify it as such without treating places I've been to as blocked areas.
I realise that this would cause the following to also show dead ends, but I can't see a way around it.
似乎缺少的是检查 X&Y 是否已被标记为解决方案的一部分,如果是,我们将中止。 (这应该是第 3.5 点上的某个地方)
如果不是一个可能存在循环的迷宫,那么某个地方会无限期地运行并炸毁堆栈
,从我读到的内容来看,该算法基于只有 1 个解决方案
R的迷宫
what seems to be missing is the check if X&Y has already been marked as part of the solution, and if so we abort. (this should be somewhere on point 3.5)
If not a maze with a possible loop somewhere would run indefinately and blow up the stack
by the way, from what I read the algorithm is based on a maze with only 1 solution
R