ExamAdda Logo

Snakes and Ladders

Easy

You are given an n × n board representing a Snakes and Ladders game.

Starting from square 1, you can roll a dice with values from 1 to 6.

After moving to a square:

  • If the square contains the start of a ladder, you move to the ladder's destination.
  • If the square contains the start of a snake, you move to the snake's destination.
  • Otherwise, you stay on that square.

Return the minimum number of dice rolls required to reach the final square.

If the final square cannot be reached, return -1.

Important

The board is numbered in a boustrophedon pattern:

1  2  3  4  5  6

12 11 10 9  8  7

13 14 15 16 17 18

...

 

The numbering direction alternates on every row.

Main Idea

This is a Graph + BFS problem.

Each board square can be considered a graph node.

From every square, there are at most six possible moves:

current + 1

current + 2

...

current + 6

 

If the destination contains a snake or ladder, immediately move to its destination.

Since every dice roll has the same cost (1), BFS gives the minimum number of rolls.

BFS Process

Start → 1

       ↓

   Try 1 to 6

       ↓

Snake/Ladder?

       ↓

Next square

 

The first time we reach the final square, we have found the minimum number of dice rolls.

Example 1

Input

6
-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

Starting from square 1, BFS explores all possible dice rolls.

One optimal route is:

1

↓

2

↓

15

↓

35

↓

...

↓

36

 

The minimum number of dice rolls required is:

4

Example 2

Input

6
-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 -1 -1 -1 -1 -1

Output

1

Explanation

From square 1, we can roll a 6 and reach square 7.

Square 7 contains a ladder leading directly to 35.

So:

1 → 7 → 35

 

The destination can then be reached optimally according to the board configuration.

Constraints

2 <= n <= 20
board.length == n
board[i].length == n
board[i][j] is either -1 or in the range [1
n²]
The board contains no snake or ladder starting on square 1.
The board contains no snake or ladder starting on the final square.

Hints:

Hint 1

Think of every square as a node in a graph.

Hint 2

From every square, you can move to at most six other squares because of the dice.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 16, 2026

Last updated

Aug 17, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output