用左手法则解迷宫 [英] solving a maze using the left hand rule

查看:569
本文介绍了用左手法则解迷宫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下sudo代码使用左侧出口规则解决迷宫 我已经使其工作大部分,但是当它碰到死胡同并返回时,我不得不使其选择一个新的方向(例如,在正方形的顶部为真,而在左右方向上为左,底和右为假的正方形的情况下),我遇到了问题第一阶段,我的代码正确地从说入口是否移到底部或右边的2个中的一个,但是当它返回时,它选择的是左方向而不是底部,我如何使它选择底部.

i am trying to solve the maze using the left hand exit rule using the the below sudo code i have got it working mostly but i am having issues with making it choose a new direction when it hits a dead end and comes back(like in the case of a square whose top is true but left, bottom and right walls are false during the first phase my code correctly moves from say if entry was left to either of the 2 that is bottom or right but when it comes back it chooses the left direction and not the bottom,how do i make it choose bottom).

有人可以建议我如何选择新方向吗?-我在所讨论的方法周围加了双星号(**),以供参考 预先感谢

could some one advise me on how to choose the new direction - i have put double asterisk (**) around the method in question for your ref thanks in advance

Set int entr to LEFT;
Set int exit to-1;
Set boolean backtrack to false;
Set currentSquare to startingSquare;
Set previousSquare to null;
Set currentSquare.onpath to true;
While (currentSquare != endingSquare) {
    **Set exit to currentSquare.getLeftHandExit(entr);**
    Set previousSquare to currentSquare;
    Set currentSquare to currentSquare.adjacentSquare(exit);
    If (backtracking is false and exit is same as entrance)
        Set backtracking to true;
        Remove previousSquare from path;
    }
    Else if backtracking is true and currentSquare is not on the path
        Set backtracking to false;
        Add previousSquare to path;
    }
    If backtracking is true, remove currentSquare from path;
    else add currentSquare to path;
    entr = currentSquare.oppositeSide(exit);
} // end of While

推荐答案

如果您始终向左转,那么只需转弯,就应该改变方向,这样您的左侧就会变成右侧.

If you always turn to the left then just turn around, and you should change the direction, so your left side changes to what was the right side.

到达下一个走廊时,您仍然会向左转.

When you get to the next hallway you will still turn left.

我认为这只是将左手放在墙上,最终您将找到出路.

I think of it as just keep your left hand on the wall, and you will eventually find your way out.

根据迷宫的复杂程度,有可能设计一个迷宫,使迷宫最终成环,因此您可能希望更改您去过的迷宫的颜色,从而可以检测到何时通过某种方式迷路了部分,或者再次重复您的路径.

Depending on how complicated your maze can be, it is possible to design a maze where you will end up in loop, so you may want to change colors of where you have been so you can detect when you went both ways through some section, or am repeating your path again.

这篇关于用左手法则解迷宫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆