@@ -53,13 +53,48 @@ The total probability the knight stays on the board is 0.0625.
53
53
### ** Python3**
54
54
55
55
``` python
56
-
56
+ class Solution :
57
+ def knightProbability (self , n : int , k : int , row : int , column : int ) -> float :
58
+ dp = [[[0 ] * n for _ in range (n)] for _ in range (k + 1 )]
59
+ for l in range (k + 1 ):
60
+ for i in range (n):
61
+ for j in range (n):
62
+ if l == 0 :
63
+ dp[l][i][j] = 1
64
+ else :
65
+ for a, b in ((- 2 , - 1 ), (- 2 , 1 ), (2 , - 1 ), (2 , 1 ), (- 1 , - 2 ), (- 1 , 2 ), (1 , - 2 ), (1 , 2 )):
66
+ x, y = i + a, j + b
67
+ if 0 <= x < n and 0 <= y < n:
68
+ dp[l][i][j] += dp[l - 1 ][x][y] / 8
69
+ return dp[k][row][column]
57
70
```
58
71
59
72
### ** Java**
60
73
61
74
``` java
62
-
75
+ class Solution {
76
+ public double knightProbability (int n , int k , int row , int column ) {
77
+ double [][][] dp = new double [k + 1 ][n][n];
78
+ int [] dirs = {- 2 , - 1 , 2 , 1 , - 2 , 1 , 2 , - 1 , - 2 };
79
+ for (int l = 0 ; l <= k; ++ l) {
80
+ for (int i = 0 ; i < n; ++ i) {
81
+ for (int j = 0 ; j < n; ++ j) {
82
+ if (l == 0 ) {
83
+ dp[l][i][j] = 1 ;
84
+ } else {
85
+ for (int d = 0 ; d < 8 ; ++ d) {
86
+ int x = i + dirs[d], y = j + dirs[d + 1 ];
87
+ if (x >= 0 && x < n && y >= 0 && y < n) {
88
+ dp[l][i][j] += dp[l - 1 ][x][y] / 8 ;
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ return dp[k][row][column];
96
+ }
97
+ }
63
98
```
64
99
65
100
### ** TypeScript**
@@ -89,6 +124,66 @@ function knightProbability(n: number, k: number, row: number, column: number): n
89
124
};
90
125
```
91
126
127
+ ### ** C++**
128
+
129
+ ``` cpp
130
+ class Solution {
131
+ public:
132
+ double knightProbability(int n, int k, int row, int column) {
133
+ vector<vector<vector<double >>> dp(k + 1, vector<vector<double >>(n, vector<double >(n)));
134
+ vector<int > dirs = {-2, -1, 2, 1, -2, 1, 2, -1, -2};
135
+ for (int l = 0; l <= k; ++l)
136
+ {
137
+ for (int i = 0; i < n; ++i)
138
+ {
139
+ for (int j = 0; j < n; ++j)
140
+ {
141
+ if (l == 0) dp[ l] [ i ] [ j] = 1;
142
+ else
143
+ {
144
+ for (int d = 0; d < 8; ++d)
145
+ {
146
+ int x = i + dirs[ d] , y = j + dirs[ d + 1] ;
147
+ if (x >= 0 && x < n && y >= 0 && y < n)
148
+ dp[ l] [ i ] [ j] += dp[ l - 1] [ x ] [ y] / 8;
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }
154
+ return dp[ k] [ row ] [ column] ;
155
+ }
156
+ };
157
+ ```
158
+
159
+ ### **Go**
160
+
161
+ ```go
162
+ func knightProbability(n int, k int, row int, column int) float64 {
163
+ dp := make([][][]float64, k+1)
164
+ dirs := []int{-2, -1, 2, 1, -2, 1, 2, -1, -2}
165
+ for l := range dp {
166
+ dp[l] = make([][]float64, n)
167
+ for i := 0; i < n; i++ {
168
+ dp[l][i] = make([]float64, n)
169
+ for j := 0; j < n; j++ {
170
+ if l == 0 {
171
+ dp[l][i][j] = 1
172
+ } else {
173
+ for d := 0; d < 8; d++ {
174
+ x, y := i+dirs[d], j+dirs[d+1]
175
+ if 0 <= x && x < n && 0 <= y && y < n {
176
+ dp[l][i][j] += dp[l-1][x][y] / 8
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ }
183
+ return dp[k][row][column]
184
+ }
185
+ ```
186
+
92
187
### ** ...**
93
188
94
189
```
0 commit comments