forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathknight-probability-in-chessboard.cpp
More file actions
27 lines (26 loc) · 1 KB
/
knight-probability-in-chessboard.cpp
File metadata and controls
27 lines (26 loc) · 1 KB
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
// Time: O(k * n^2)
// Space: O(n^2)
class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
static const vector<pair<int, int>> directions =
{{ 1, 2}, { 1, -2}, { 2, 1}, { 2, -1},
{-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};
vector<vector<vector<double>>> dp(2, vector<vector<double>>(N, vector<double>(N, 1.0l)));
for (int step = 1; step <= K; ++step) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
dp[step % 2][i][j] = 0;
for (const auto& direction : directions) {
auto rr = i + direction.first;
auto cc = j + direction.second;
if (0 <= cc && cc < N && 0 <= rr && rr < N) {
dp[step % 2][i][j] += 0.125l * dp[(step - 1) % 2][rr][cc];
}
}
}
}
}
return dp[K % 2][r][c];
}
};