墙跟随迷宫解算器
我的迷宫求解算法遇到了一些问题。我正在尝试实施左手法则。
public Direction move(View v) {
if (!wallExistsToLeft(v)) {
turnLeft();
} else if (v.mayMove(direction)) {
return direction;
} else if (!wallExistsToRight(v)){
turnRight();
} else {
turnAround();
}
return direction;
}
方向始终设置为迷宫解算器面临的当前方向。
turnX 根据您当前面对的方向更改方向
move 函数返回一个方向,迷宫解算器在该方向上移动 1 个空间。
有人能指出我正确的方向吗?我确信有一些简单的递归方法可以实现这一点,但我似乎无法解决。
目前我在这两个测试中失败了:
任何帮助将不胜感激。
I'm having some trouble with my maze solving algorithm. I'm trying to implement the left hand rule.
public Direction move(View v) {
if (!wallExistsToLeft(v)) {
turnLeft();
} else if (v.mayMove(direction)) {
return direction;
} else if (!wallExistsToRight(v)){
turnRight();
} else {
turnAround();
}
return direction;
}
direction is always set to the current direction the maze solver is facing.
turnX changes the direction based on the direction you're currently facing
The move function returns a direction in which the maze solver moves 1 space in that direction.
Can anyone point me in the right direction? I'm sure there is some simple recursive way that this can be implemented but I can't seem to work it out.
Currently I'm failing on these two tests:
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
左手定则仅适用于起始点和目标点紧邻同一墙体连接组件的墙段的情况。如果房间中间有一根柱子,而你从它旁边开始,你总是会绕着它走。
第二个问题来自开放空间。如果没有墙可依附,那么算法将永远在原地踏步。当提出这种算法时,通常人们会想到走廊是否狭窄。
所以无论你的实现是否正确,测试用例都不能通过简单的左手法则通过。
the left-hand-rule is only applicable if start and goal are next to wall segments of the same connected component of walls. If there is a pillar in the middle of a room and you start next to it you will always walk around it.
the second problem comes from open spaces. If there is no wall to cling to, then the algorithm will always walk in circles. Usually one thinks if narrow corridors when suggesting this algorithm.
So regardless of whether your implementation is correct, the testcases cannot be passed by a simple left-hand-rule.
从你的照片来看,你总是右转。
从您的代码来看,这表明 wallExistsToLeft(v) 始终返回 true,而 v.mayMove(direction) 始终返回 false。
From your pictures, it looks like you always turn right.
Which, from your code, would indicate that wallExistsToLeft(v) always returns true, and v.mayMove(direction) always returns false.