Wall follow maze solver
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 facin开发者_如何转开发g
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.
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.
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.
精彩评论