Snakes and Ladders

Given an n x n board for the Snakes and Ladders game, return the minimum number of dice throws required to reach the final cell starting from cell 1. If the cell contains a snake or ladder, you must move to its destination. Pattern focus: BFS. Model each board cell as a node and each dice throw as one edge.

Input Format

board = 2D game board

Output Format

minimum dice throws to reach final cell

Constraints

  • 2 <= n <= 20
  • Each cell is -1 or a destination cell number.

Examples

Example 1:

Input:

board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]

Output:

4

Explanation:

A standard board where the minimum number of moves is 4.

Example 2:

Input:

board = [[-1,-1],[-1,3]]

Output:

1

Explanation:

A ladder can take you directly to the end in one move.

Example 3:

Input:

board = [[-1,-1],[-1,-1]]

Output:

1

Explanation:

On a 2x2 board, one throw can reach the final cell.

Loading...
Snakes and Ladders - Graph Traversal DSA Problem