Snake and Ladder problem solution using BFS (breadth first search). The problem is to find the minimum number of dice throws or minimum move to reach to the top right most corner. The movement of dice would start from position 1 and we have to reach at 31th position with minimum moves.

snake and ladder problem solution using BFS

How to solve

Test Cases:

The first line contains the number of tests, T. T testcases follow.

For each testcase, the first line contain N(Number of ladders) and after that N line follow. Each of the N line contain 2 integer representing the starting point and the ending point of a ladder respectively.

The next line contain integer M(Number of snakes) and after that M line follow. Each of the M line contain 2 integer representing the starting point and the ending point of a snake respectively.

 

Ref:

https://www.hackerrank.com/challenges/the-quickest-way-up