Snakes and Ladders - Complete Solution Guide
Snakes and Ladders is LeetCode problem 909, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an n x n integer matrix board where the cells are labeled from 1 to n 2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0] ) and alternating direction each row. You start on square 1 of the board. In each move, starting from square curr , do the following: Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n 2 )] . This choice simulates the result of a standard 6-sided die roll : i.e., there are always at most
Detailed Explanation
The Snakes and Ladders problem presents a game board represented by an n x n matrix. The board is numbered from 1 to n^2 in a boustrophedon (snake-like) pattern. The goal is to find the minimum number of dice rolls to reach the square n^2, starting from square 1. Each roll allows you to move from your current square `curr` to a square `next` within the range `[curr + 1, min(curr + 6, n^2)]`. If a square has a snake or a ladder (indicated by a value other than -1), you must move to the destination of that snake or ladder. The objective is to find the shortest path to the final square.
Solution Approach
The solution utilizes Breadth-First Search (BFS) to find the shortest path from square 1 to square n^2. A queue is used to store the squares to be visited, along with the number of moves taken to reach them. The algorithm explores possible moves from each square (simulating a dice roll), checking for snakes or ladders at each step, and ensuring visited squares are not revisited.
Step-by-Step Algorithm
- Step 1: Initialize a queue with the starting square (1) and the number of moves (0). Also, initialize a set to keep track of visited squares to avoid cycles.
- Step 2: While the queue is not empty, dequeue the current square and its corresponding move count.
- Step 3: If the current square is the target square (n^2), return the move count.
- Step 4: Iterate through possible dice rolls (1 to 6) from the current square, calculating the next potential square.
- Step 5: If the next square is within the board's bounds, determine its row and column coordinates using the `get_coords` function (which accounts for the boustrophedon pattern).
- Step 6: Check if the next square has a snake or ladder. If it does, update the destination to the snake/ladder's target square; otherwise, the destination is the originally calculated next square.
- Step 7: If the destination square has not been visited, add it to the visited set and enqueue it with an incremented move count.
- Step 8: If the queue becomes empty and the target square is not reached, it means there's no path, so return -1.
Key Insights
- Insight 1: The core problem is a shortest path problem, ideally suited for Breadth-First Search (BFS).
- Insight 2: Mapping the 1D square number to 2D board coordinates is crucial and requires handling the boustrophedon pattern correctly.
- Insight 3: Snakes and ladders act as 'shortcuts' or 'jumps' in the graph, but only one jump is allowed per turn. Avoiding cycles is important to prevent infinite loops.
Complexity Analysis
Time Complexity: O(N^2)
Space Complexity: O(N^2)
Topics
This problem involves: Array, Breadth-First Search, Matrix.
Companies
Asked at: Anduril, Cisco, National Instruments, PhonePe, Pinterest, Zomato, thoughtspot.