Practice › Graphs
Snakes and Ladders
Medium · Graphs · Asked at Amazon, Google, Microsoft, Flipkart
An n × n board has squares numbered 1 to n² in a zigzag starting from the bottom-left: the bottom row goes left to right, the row above right to left, and so on. Each cell holds -1 or the destination of a snake or ladder starting on that square.
You start on square 1. In one move you pick a result from 1 to 6 and go to square x + result (at most n²); if that square has a snake or ladder, you must take it — but only that one, even if its destination also has one. Print the minimum number of moves to reach square n², or -1 if it is impossible.
Input
First line: n. Next n lines: the board rows from top to bottom, n values each.
Output
The minimum number of moves, or -1.
Constraints
- 2 ≤ n ≤ 20
- squares 1 and n² have no snake or ladder
BFS over squares 1 … n², where each square has up to 6 neighbours. Convert a square number to a cell with row = n − 1 − (s − 1) / n and the column reversed on every other row.
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
Example 2
Input
2 -1 -1 -1 3
Output
1
Hints
- Every move costs one turn — shortest number of moves means BFS.
- From square x you can land on x + 1 … x + 6, then follow a snake or ladder if there is one.
- The tricky part is mapping a square number to its cell on the zigzag board.
Approach
For square s (1-based), its row from the bottom is rr = (s − 1) / n and its column is (s − 1) % n, reversed when rr is odd; the board row in the input is n − 1 − rr. BFS from square 1: for each die value d, compute y = x + d (stop at n²), replace y by the jump target if the cell holds one, and enqueue y if unseen. Only one jump per move is taken, which this naturally does. dist[n²] is the answer, or −1.
Complexity: O(6 n²) time · O(n²) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> g(n, vector<int>(n));
for (auto &row : g)
for (auto &x : row) cin >> x;
auto cell = [&](int s) {
int rr = (s - 1) / n, c = (s - 1) % n;
if (rr % 2) c = n - 1 - c;
return g[n - 1 - rr][c];
};
int N = n * n;
vector<int> dist(N + 1, -1);
queue<int> q;
dist[1] = 0;
q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int d = 1; d <= 6 && x + d <= N; d++) {
int y = x + d;
if (cell(y) != -1) y = cell(y);
if (dist[y] < 0) {
dist[y] = dist[x] + 1;
q.push(y);
}
}
}
cout << dist[N] << "\n";
return 0;
}Python solution
import sys
from collections import deque
data = list(map(int, sys.stdin.read().split()))
n = data[0]
g = [data[1 + i * n : 1 + (i + 1) * n] for i in range(n)]
def cell(s):
rr, c = (s - 1) // n, (s - 1) % n
if rr % 2:
c = n - 1 - c
return g[n - 1 - rr][c]
N = n * n
dist = [-1] * (N + 1)
dist[1] = 0
q = deque([1])
while q:
x = q.popleft()
for d in range(1, 7):
y = x + d
if y > N:
break
jump = cell(y)
if jump != -1:
y = jump
if dist[y] < 0:
dist[y] = dist[x] + 1
q.append(y)
print(dist[N])Java solution
import java.io.*;
import java.util.*;
public class Main {
static int n;
static int[][] g;
static int cell(int s) {
int rr = (s - 1) / n, c = (s - 1) % n;
if (rr % 2 == 1) c = n - 1 - c;
return g[n - 1 - rr][c];
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
n = (int) in.nval;
g = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
in.nextToken();
g[i][j] = (int) in.nval;
}
int N = n * n;
int[] dist = new int[N + 1];
Arrays.fill(dist, -1);
ArrayDeque<Integer> q = new ArrayDeque<>();
dist[1] = 0;
q.add(1);
while (!q.isEmpty()) {
int x = q.poll();
for (int d = 1; d <= 6 && x + d <= N; d++) {
int y = x + d;
if (cell(y) != -1) y = cell(y);
if (dist[y] < 0) {
dist[y] = dist[x] + 1;
q.add(y);
}
}
}
System.out.println(dist[N]);
}
}