Search This Blog

Tuesday, 2 August 2016

Maze Solving Algorithm



This maze solving algorithm works on the principle of Backtracking. It is implemented in Java.

The main idea is to solve the problem is move from the starting point in all the possible directions until we reach the goal. While moving in a particular direction, when ever the path splits(i.e., a node is encountered) one of the path is chosen. We move on the same path until we encounter a dead end(or a leaf). When a leaf is reached, we backtrack to the last encountered node and chose another path. This procedure is followed recursively until either we reach the goal or we reach the first encountered node again by backtracking from all possible ways and hence no solution to the maze exists. When we have reached our goal, we can mark the path used.

The code is explained with comments. Further for better understanding, i have used some print statements as comments. The code can be tested on other maze problem too by simply changing the maze array used in the code.

Open link to go to View Source Code.
Click here to download class file.



The output of the code is given under:

   

No comments:

Post a Comment