945. Snakes and Ladders
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of dice rolls to travel from square 1 to square n*n on a game board. The movement is determined by a 6-sided die. Some squares contain snakes or ladders, which act as teleporters. This problem can be modeled as finding the shortest path in an unweighted graph. The squares of the board are the nodes, and a valid move (a dice roll) between squares represents a directed edge with a weight of 1. Breadth-First Search (BFS) is the perfect algorithm for finding the shortest path in such a graph, as it explores the graph level by level, guaranteeing that the first time we reach the destination, it is via the shortest path.
Solution
Snakes and Ladders in Python
import collections
class Solution:
def snakesAndLadders(self, board: list[list[int]]) -> int:
n = len(board)
target = n * n
def get_coords(s):
# Convert 1-indexed square number to 0-indexed coordinates
s -= 1
r_from_bottom = s // n
r = n - 1 - r_from_bottom
c = s % n
if r_from_bottom % 2 == 1: # Odd row from bottom, right-to-left
c = n - 1 - c
return r, c
# dist[i] stores the minimum moves to reach square i.
# -1 means not visited yet.
dist = [-1] * (target + 1)
q = collections.deque([1])
dist[1] = 0
while q:
curr = q.popleft()
if curr == target:
return dist[curr]
# Explore next moves from a dice roll (1 to 6)
for i in range(1, 7):
next_square = curr + i
if next_square > target:
break
r, c = get_coords(next_square)
final_dest = next_square
if board[r][c] != -1:
final_dest = board[r][c]
if dist[final_dest] == -1: # If not visited
dist[final_dest] = dist[curr] + 1
q.append(final_dest)
return -1 # Target is not reachable
Snakes and Ladders in Java
class Solution {
public int snakesAndLadders(int[][] board) {
int n = board.length;
int target = n * n;
// dist[i] stores the minimum moves to reach square i.
// -1 means not visited yet.
int[] dist = new int[target + 1];
Arrays.fill(dist, -1);
Queue<Integer> q = new LinkedList<>();
dist[1] = 0;
q.add(1);
while (!q.isEmpty()) {
int curr = q.poll();
if (curr == target) {
return dist[curr];
}
// Explore next moves from a dice roll (1 to 6)
for (int i = 1; i <= 6; i++) {
int next = curr + i;
if (next > target) {
break;
}
int[] coords = getCoords(next, n);
int r = coords[0];
int c = coords[1];
int finalDest = next;
if (board[r][c] != -1) {
finalDest = board[r][c];
}
if (dist[finalDest] == -1) { // If not visited
dist[finalDest] = dist[curr] + 1;
q.add(finalDest);
}
}
}
return -1; // Target is not reachable
}
// Helper function to convert square number to coordinates
private int[] getCoords(int s, int n) {
int r_from_bottom = (s - 1) / n;
int c = (s - 1) % n;
if (r_from_bottom % 2 == 1) { // Odd row from bottom, right-to-left
c = n - 1 - c;
}
int r = n - 1 - r_from_bottom;
return new int[]{r, c};
}
}
Snakes and Ladders in C++
class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
int n = board.size();
int target = n * n;
// Helper function to convert square number to coordinates
auto getCoords = [&](int s) -> pair<int, int> {
int r_from_bottom = (s - 1) / n;
int c = (s - 1) % n;
if (r_from_bottom % 2 == 1) { // Odd row from bottom, right-to-left
c = n - 1 - c;
}
int r = n - 1 - r_from_bottom;
return {r, c};
};
// dist[i] stores the minimum moves to reach square i.
// -1 means not visited yet.
vector<int> dist(target + 1, -1);
queue<int> q;
dist[1] = 0;
q.push(1);
while (!q.empty()) {
int curr = q.front();
q.pop();
if (curr == target) {
return dist[curr];
}
// Explore next moves from a dice roll (1 to 6)
for (int i = 1; i <= 6; ++i) {
int next = curr + i;
if (next > target) {
break;
}
auto [r, c] = getCoords(next);
int final_dest = next;
if (board[r][c] != -1) {
final_dest = board[r][c];
}
if (dist[final_dest] == -1) { // If not visited
dist[final_dest] = dist[curr] + 1;
q.push(final_dest);
}
}
}
return -1; // Target is not reachable
}
};
Snakes and Ladders in TypeScript
function snakesAndLadders(board: number[][]): number {
const n = board.length;
const target = n * n;
// Helper function to convert square number to coordinates
const getCoords = (s: number): [number, number] => {
const r_from_bottom = Math.floor((s - 1) / n);
let c = (s - 1) % n;
if (r_from_bottom % 2 === 1) { // Odd row from bottom, right-to-left
c = n - 1 - c;
}
const r = n - 1 - r_from_bottom;
return [r, c];
};
// dist[i] stores the minimum moves to reach square i.
// -1 means not visited yet.
const dist: number[] = new Array(target + 1).fill(-1);
const q: number[] = [];
dist[1] = 0;
q.push(1);
while (q.length > 0) {
const curr = q.shift()!;
if (curr === target) {
return dist[curr];
}
// Explore next moves from a dice roll (1 to 6)
for (let i = 1; i <= 6; i++) {
let next = curr + i;
if (next > target) {
break;
}
const [r, c] = getCoords(next);
let finalDest = next;
if (board[r][c] !== -1) {
finalDest = board[r][c];
}
if (dist[finalDest] === -1) { // If not visited
dist[finalDest] = dist[curr] + 1;
q.push(finalDest);
}
}
}
return -1; // Target is not reachable
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.