Nanduva
Nanduva
find a path if it exists. But such an approach will have exponential complexity and
will not scale well.
However, it's possible to customize the brute force solution mentioned above, by
backtracking and marking visited nodes, to obtain a path in a reasonable time. This
algorithm is also known as Depth-first search.
For each node, we traverse each direction in order: right, bottom, left, top.
freestar
In 1(b), we explore a path and hit the wall. Then we backtrack till a node is found
which has non-wall neighbors, and explore another path as shown in 1(c).
We again hit the wall and repeat the process to finally find the exit, as shown in
1(d)
public List<Coordinate> solve(Maze maze) {
List<Coordinate> path = new ArrayList<>();
if (
explore(
maze,
maze.getEntry().getX(),
maze.getEntry().getY(),
path
)
) {
return path;
}
return Collections.emptyList();
}