Knight Probability In Chessboard Problem
Description
LeetCode Problem 688.
On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly k moves or has moved off the chessboard.
Return the probability that the knight remains on the board after it has stopped moving.
Example 1:
Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.
Example 2:
Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000
Constraints:
- 1 <= n <= 25
- 0 <= k <= 100
- 0 <= row, column <= n
Solution
Approach: Dynamic Programming over Moves
Simulating every sequence of moves would mean 8k paths. But the future of the knight depends only on where it stands now, not on how it got there, so we can track a probability for each cell instead of individual paths.
Let prob[r][c] be the probability that, after the moves made so far, the knight is still on the board and stands on (r, c). Before any move, prob[row][column] = 1 and every other cell is 0.
For each of the k moves, build a fresh grid nextProb:
- For every cell
(r, c)and each of the 8 knight moves(dr, dc), the knight goes to(r + dr, c + dc)with probability 1/8. - If that target is on the board, add
prob[r][c] / 8tonextProbthere. If it is off the board, that share is lost: the knight stops and never comes back.
After k rounds, the answer is the sum of all entries of prob.
Use a new grid for each round instead of updating prob in place. Otherwise a cell's probability could be moved twice in the same round.
Example 1: from (0, 0) on a 3 x 3 board only 2 of 8 moves stay on, giving 1/4 after one move, and again 2 of 8 from each of those cells, giving 1/16 = 0.0625.
Complexity: O(k · n2) time, as each round looks at n2 cells and 8 moves each. O(n2) space for the two grids.
class Solution:
def knightProbability(self, n: int, k: int, row: int, column: int) -> float:
moves = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)]
# prob[r][c] = probability that the knight is on (r, c) after the moves made so far
prob = [[0.0] * n for _ in range(n)]
prob[row][column] = 1.0
for _ in range(k):
next_prob = [[0.0] * n for _ in range(n)]
for r in range(n):
for c in range(n):
if prob[r][c] == 0:
continue
for dr, dc in moves:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < n: # moves off the board are simply lost
next_prob[nr][nc] += prob[r][c] / 8
prob = next_prob
return sum(sum(line) for line in prob)
Now write it from memory
Close the write-up and solve it yourself against the tests.