Find the Fewest Rolls through a Snakes-and-Ladders Board

Quick Overview

Find the Fewest Rolls through a Snakes-and-Ladders Board. The n-by-n board labels squares1 through n squared, beginning at the bottom-left cell and reversing horizontal direction on every row upward.

Find the Fewest Rolls through a Snakes-and-Ladders Board

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The n-by-n board labels squares1 through n squared, beginning at the bottom-left cell and reversing horizontal direction on every row upward. Start on1. A roll chooses any label from current+1 through min(current+6,n squared). If that landing cell contains a destination instead of `-1`, take that one mandatory snake or ladder. Do not follow another jump from its destination during the same roll. Return the minimum rolls to reach the final square, or `-1` if unreachable. Squares1 and n squared have no outgoing jump. ```cpp class Solution { public: int snakesAndLadders(vector<vector<int>>& board); }; ``` ### Examples **Example 1:** ```text Input: board = [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]] Output: 2 Explanation: Roll from1 to7, then to9. One roll can reach only labels2 through7. ``` **Example 2:** ```text Input: board = [[-1,-1,-1],[-1,-1,-1],[-1,9,-1]] Output: 1 Explanation: Choose square2 and take its ladder directly to9. ``` ### Constraints - `n == board.length == board[i].length`; `2 <= n <= 20`. - Every entry is `-1` or a label from1 through n squared. - Labels1 and n squared are not starts of snakes or ladders. ### Contract considerations The die is a choice over at most six next labels; this asks for a minimum, not an expected number of random rolls. ```hint Read the boundary condition A BFS state should describe the square after the one allowed jump has finished. ``` ### Boundary reminders The die is a choice over at most six next labels; this asks for a minimum, not an expected number of random rolls.

Overview: Find the Fewest Rolls through a Snakes-and-Ladders Board. The n-by-n board labels squares1 through n squared, beginning at the bottom-left cell and reversing horizontal direction on every row upward.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The n-by-n board labels squares1 through n squared, beginning at the bottom-left cell and reversing horizontal direction on every row upward. Start on1. A roll chooses any label from current+1 through min(current+6,n squared). If that landing cell contains a destination instead of -1, take that one mandatory snake or ladder. Do not follow another jump from its destination during the same roll. Return the minimum rolls to reach the final square, or -1 if unreachable. Squares1 and n squared have no outgoing jump.

class Solution {
public:
    int snakesAndLadders(vector<vector<int>>& board);
};

Examples

Example 1:

Input: board = [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]]
Output: 2
Explanation: Roll from1 to7, then to9. One roll can reach only labels2 through7.

Example 2:

Input: board = [[-1,-1,-1],[-1,-1,-1],[-1,9,-1]]
Output: 1
Explanation: Choose square2 and take its ladder directly to9.

Constraints

  • n == board.length == board[i].length ; 2 <= n <= 20 .
  • Every entry is -1 or a label from1 through n squared.
  • Labels1 and n squared are not starts of snakes or ladders.

Contract considerations

The die is a choice over at most six next labels; this asks for a minimum, not an expected number of random rolls.

Boundary reminders

The die is a choice over at most six next labels; this asks for a minimum, not an expected number of random rolls.