BFS to solve 'Snake and Ladder' Problem.
Howdy folks! Thank you for visiting my blog once again. 😊 Today, let’s implement another interesting algorithm, that is Snake and Ladder board in Java. Consider below snake & ladder board, fig.1 Problem Statement: In above board, there are 100 cells. Considering player has a full control over a dice he throws. write an algorithm to find out minimum number of throws required to reach to the last cell which is 100 th cell from the 1 st cell. Approach: Let’s convert above board into a graph (Unaware about graph DS? click on mypost ) having 100 nodes. Each node is connected to maximum 6 nodes as dice has maximum six numbers. In between if I have the ladder then the top of ladder node is directly connected to the source node. So, in our board Node -1 is connected to the {2,3,4, 25 ,6,7}. Also, if there is a snake then it will take you to the bottom of it. So, in our board if I’m at node 6 then I can go to node {7,8,9,29,30, 1...