0% found this document useful (0 votes)
19 views1 page

Nanduva

The document discusses depth-first search, an algorithm that explores all paths from a starting node in a maze to find the exit. It does this by marking visited nodes to avoid getting stuck in loops, and backtracking from dead ends to try other paths. An example applies this algorithm to a sample maze, exploring different paths until it finds the exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Nanduva

The document discusses depth-first search, an algorithm that explores all paths from a starting node in a maze to find the exit. It does this by marking visited nodes to avoid getting stuck in loops, and backtracking from dead ends to try other paths. An example applies this algorithm to a sample maze, exploring different paths until it finds the exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

One fairly obvious approach is to explore all possible paths, which will ultimately

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.

This algorithm can be outlined as:

If we're at the wall or an already visited node, return failure


Else if we're the exit node, then return success
Else, add the node in path list and recursively travel in all four directions. If
failure is returned, remove the node from the path and return failure. Path list
will contain a unique path when exit is found
Let's apply this algorithm to the maze shown in Figure-1(a), where S is the
starting point, and E is the exit.

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();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy