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.
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:
1
2
3
4
Input:
n = 1, k = 0, row = 0, column = 0
Output:
1.00000
privatestaticfinalint[][] DIRS = {
{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
{1, 2}, {1, -2}, {-1, 2}, {-1, -2}
};
publicdoubleknightProbability(int n, int k, int row, int column) {
return dfs(n, k, row, column);
}
publicdoubledfs(int n, int k, int r, int c) {
if (r < 0 || r > n - 1 || c < 0 || c > n-1) {
return 0;
}
if (k == 0) {
return 1;
}
double probability = 0;
for(int[] dir: DIRS) {
probability += 0.125* dfs(n, k - 1, r + DIRS[0], c + DIRS[1]);
}
return probability;
}
privatestaticfinalint[][] DIRS =newint[][]{{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}};
publicdoubleknightProbability(int n, int k, int row, int column) {
return dfs(n, k, row, column, newdouble[n][n][k + 1]);
}
publicdoubledfs(int n, int k, int r, int c, double[][][] dp) {
if (r < 0 || r > n - 1 || c < 0 || c > n-1) {
return 0;
}
if (k == 0) {
return 1;
}
if(dp[r][c][k]!= 0) {
return dp[r][c][k];
}
double probability = 0;
for(int[] dir: DIRS) {
probability += 0.125*dfs(n, k - 1, r + dir[0], c + dir[1], dp);
}
dp[r][c][k]= probability;
return probability;
}
defknightProbability(n, k, row, column):
# Possible moves of the knight moves = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]
memo = {}
defdfs(n, k, row, column):
# If out of bounds, return 0if row <0or row >= n or column <0or column >= n:
return0# If no more moves left, return 1if k ==0:
return1# Check if the result is already calculatedif (k, row, column) in memo:
return memo[(k, row, column)]
probability =0for move in moves:
next_row, next_column = row + move[0], column + move[1]
probability += dfs(n, k -1, next_row, next_column) /8 memo[(k, row, column)] = probability
return probability
return dfs(n, k, row, column)
defknightProbability(n, k, row, column):
# Possible moves of the knight moves = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]
# dp array to store the probabilities dp = [[[0] * n for _ in range(n)] for _ in range(k +1)]
dp[0][row][column] =1# Initial position, 0 moves leftfor step in range(1, k +1):
for r in range(n):
for c in range(n):
for move in moves:
prev_row, prev_column = r + move[0], c + move[1]
if0<= prev_row < n and0<= prev_column < n:
dp[step][r][c] += dp[step -1][prev_row][prev_column] /8# Calculate the final probability final_probability =0for r in range(n):
for c in range(n):
final_probability += dp[k][r][c]
return final_probability