forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
37 lines (37 loc) · 992 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function knightProbability(
n: number,
k: number,
row: number,
column: number,
): number {
let dp = Array.from({ length: k + 1 }, v =>
Array.from({ length: n }, w => new Array(n).fill(0)),
);
const directions = [
[-2, -1],
[-2, 1],
[-1, -2],
[-1, 2],
[1, -2],
[1, 2],
[2, -1],
[2, 1],
];
for (let depth = 0; depth <= k; depth++) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (!depth) {
dp[depth][i][j] = 1;
} else {
for (let [dx, dy] of directions) {
let [x, y] = [i + dx, j + dy];
if (x >= 0 && x < n && y >= 0 && y < n) {
dp[depth][i][j] += dp[depth - 1][x][y] / 8;
}
}
}
}
}
}
return dp[k][row][column];
}