墙跟随迷宫解算器

发布于 2024-10-24 05:09:26 字数 599 浏览 1 评论 0原文

我的迷宫求解算法遇到了一些问题。我正在尝试实施左手法则。

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:

enter image description here

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

娇妻 2024-10-31 05:09:27

左手定则仅适用于起始点和目标点紧邻同一墙体连接组件的墙段的情况。如果房间中间有一根柱子,而你从它旁边开始,你总是会绕着它走。
第二个问题来自开放空间。如果没有墙可依附,那么算法将永远在原地踏步。当提出这种算法时,通常人们会想到走廊是否狭窄。
所以无论你的实现是否正确,测试用例都不能通过简单的左手法则通过。

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.

生生漫 2024-10-31 05:09:26

从你的照片来看,你总是右转。

从您的代码来看,这表明 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.

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