Knight Probability in Chessboard (LeetCode 688): Solution and Explanation
A knight starts on a square of an n x n board and makes k random moves, choosing each of its 8 jumps with equal chance. Once it jumps off the board it is gone. Return the probability it is still on the board after k moves.
Intuition
Listing every random path is impossible: there are 8^k of them. But where the knight can go next depends only on the square it stands on, not on the route it took to get there.
So merge every path that reaches the same square and keep one number per square: the probability that the knight stands there after the current number of moves.
One move takes each square's probability and splits it into 8 equal shares, one per knight jump. Shares whose jump lands off the board are dropped. Shares that land on the board add into the target square of the next layer.
After k layers, the probability still on the board is just the sum of the grid. Everything that fell off was never added back.
Approach: Forward probability DP over board layers
- Create an n x n grid
dpof zeros and setdp[row][column] = 1. - Repeat k times: create an empty grid
nxt. - For every square (r, c) with
dp[r][c] > 0and each of the 8 knight moves (dr, dc), if (r + dr, c + dc) is on the board, adddp[r][c] / 8tonxt[r + dr][c + dc]. - Replace
dpwithnxt. - Return the sum of every value in
dp.
Step-by-step example
n = 3, k = 2, row = 0, column = 0
- dp starts with 1 on (0, 0) and 0 everywhere else.
- Move 1: from (0, 0) each jump carries 1 / 8. Only (1, 2) and (2, 1) are on the board, so each gets 0.125 and the other 6 shares fall off. On-board total: 0.25.
- Move 2: from (1, 2) the jumps to (0, 0) and (2, 0) stay on the board, each receiving 0.125 / 8 = 0.015625. From (2, 1) the jumps to (0, 0) and (0, 2) stay on, also 0.015625 each.
- The new grid holds 0.03125 on (0, 0) and 0.015625 on (0, 2) and (2, 0). Their sum is 0.0625, the answer.
Code: Python, Java, C++
1class Solution:
2 def knightProbability(self, n: int, k: int, row: int, column: int) -> float:
3 moves = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)]
4 dp = [[0.0] * n for _ in range(n)]
5 dp[row][column] = 1.0
6 for _ in range(k):
7 nxt = [[0.0] * n for _ in range(n)]
8 for r in range(n):
9 for c in range(n):
10 if dp[r][c] == 0:
11 continue
12 for dr, dc in moves:
13 nr, nc = r + dr, c + dc
14 if 0 <= nr < n and 0 <= nc < n:
15 nxt[nr][nc] += dp[r][c] / 8.0
16 dp = nxt
17 return sum(map(sum, dp))1class Solution {
2 public double knightProbability(int n, int k, int row, int column) {
3 int[][] moves = {{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, {1, -2}, {2, -1}, {-1, -2}, {-2, -1}};
4 double[][] dp = new double[n][n];
5 dp[row][column] = 1.0;
6 for (int step = 0; step < k; step++) {
7 double[][] nxt = new double[n][n];
8 for (int r = 0; r < n; r++) {
9 for (int c = 0; c < n; c++) {
10 if (dp[r][c] == 0) continue;
11 for (int[] m : moves) {
12 int nr = r + m[0], nc = c + m[1];
13 if (nr >= 0 && nr < n && nc >= 0 && nc < n) {
14 nxt[nr][nc] += dp[r][c] / 8.0;
15 }
16 }
17 }
18 }
19 dp = nxt;
20 }
21 double total = 0;
22 for (double[] line : dp) for (double v : line) total += v;
23 return total;
24 }
25}1class Solution {
2public:
3 double knightProbability(int n, int k, int row, int column) {
4 int moves[8][2] = {{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, {1, -2}, {2, -1}, {-1, -2}, {-2, -1}};
5 vector<vector<double>> dp(n, vector<double>(n, 0.0));
6 dp[row][column] = 1.0;
7 for (int step = 0; step < k; step++) {
8 vector<vector<double>> nxt(n, vector<double>(n, 0.0));
9 for (int r = 0; r < n; r++) {
10 for (int c = 0; c < n; c++) {
11 if (dp[r][c] == 0) continue;
12 for (auto& m : moves) {
13 int nr = r + m[0], nc = c + m[1];
14 if (nr >= 0 && nr < n && nc >= 0 && nc < n) {
15 nxt[nr][nc] += dp[r][c] / 8.0;
16 }
17 }
18 }
19 }
20 dp = nxt;
21 }
22 double total = 0;
23 for (auto& line : dp) for (double v : line) total += v;
24 return total;
25 }
26};Time and space complexity
O(k * n^2)
Each of the k moves visits all n^2 squares and tries 8 jumps from each, which is constant work per square.
O(n^2)
Only the current grid and the next grid are stored.
Other approaches
Memoized recursion (time O(k * n^2), space O(k * n^2))
Define f(r, c, moves) as the probability of surviving the remaining moves from (r, c). It is 0 off the board, 1 when no moves remain, and otherwise the average of f over the 8 jumps. Memoizing gives the same O(k n^2) time but uses O(k n^2) memory.
Monte Carlo simulation (time O(trials * k), space O(1))
Simulate many random walks and count how many survive. It only estimates the answer, and getting the 1e-5 precision a judge expects needs an enormous number of trials.
Common mistakes
- Updating dp in place. A square's share for this move must come from the previous layer only, so write into a fresh grid.
- Dividing by the number of on-board moves instead of 8. The knight picks among all 8 moves, including the ones that leave the board.
- Counting paths with integers and dividing by 8^k at the end. 8^100 overflows every integer type; divide by 8 on each move instead.
- Forgetting k = 0. With no moves the knight is still on the board, so the answer is 1.
Frequently asked questions
Why does the probability on the board only go down?
Each move splits a square's probability into 8 shares and keeps only the shares that land on the board. Nothing ever comes back from off the board, so the total can only stay the same or shrink.
Why is it enough to store one number per square?
Two different paths that end on the same square have exactly the same future, so their probabilities can be added together. That collapses 8^k paths into n^2 numbers per layer.
Can I skip squares with zero probability?
Yes. A square with probability 0 contributes nothing to the next layer, so skipping it is a cheap speedup, especially in the first few moves when only a few squares are reachable.
Is a backward DP also correct?
Yes. Computing the survival probability from every square with m moves left, and reading the value at (row, column) after k rounds, gives the same answer because knight moves are symmetric.