solving a maze using the left hand rule

拈花ヽ惹草 提交于 2019-12-24 07:11:01

问题


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

回答1:


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.




回答2:


Use a backtracking system; whether it is by using a recursive method (not preferrable) or a stack to walk back the step. Ideally, you'd also set markers in every junction that your algorithm choose a way in, so you do not choose the same path again (choose only unmarked paths in a given junction)

Wikipedia has some nice pseudocode on how to achieve this. Pay attention to the "Recursive backtracker" algorithm, replacing the "Choose randomly one of the unvisited neighbours" by "Choose left turn from one of the unvisited neighbours" (meaning choosing clock-wise from the left cell).

Also, check out this e-book about recursivity.

I'd go for something like (untested code) :

maze.clearAllVisited();
Stack<Point> paths = new Stack<Point>();
int x = maze.getStartX();
int y = maze.getStartY();
while (!maze.isExit(x, y)) {
   maze.setVisited(x, y);
   if (maze.canGoWest(x, y)) {    // check if west cell is accessible from x,y and has not been visited
      paths.push(new Point(x, y));
      x--;
   } else if (maze.canGoNorth(x, y)) { // check if north cell is accessible from x,y and has not been visited
      paths.push(new Point(x, y));
      y--;
   } else if (maze.canGoEast(x, y)) {  // ...
      paths.push(new Point(x, y));
      x++;
   } else if (maze.canGoSouth(x, y)) { // ...
      paths.push(new Point(x, y));
      y++;
   } else {
      if (paths.isEmpty()) {
         break;  // no more path for backtracking, exit (aka no solution for maze)
      }
      // dead end! go back!
      Point last = stack.pop();
      x = last.x;
      y = last.y;
   }   
}


来源:https://stackoverflow.com/questions/4362657/solving-a-maze-using-the-left-hand-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!