forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
27 lines (27 loc) · 924 Bytes
/
Solution.cpp
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
class Solution {
public:
double knightProbability(int n, int k, int row, int column) {
vector<vector<vector<double>>> dp(k + 1, vector<vector<double>>(n, vector<double>(n)));
vector<int> dirs = {-2, -1, 2, 1, -2, 1, 2, -1, -2};
for (int l = 0; l <= k; ++l)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (l == 0) dp[l][i][j] = 1;
else
{
for (int d = 0; d < 8; ++d)
{
int x = i + dirs[d], y = j + dirs[d + 1];
if (x >= 0 && x < n && y >= 0 && y < n)
dp[l][i][j] += dp[l - 1][x][y] / 8;
}
}
}
}
}
return dp[k][row][column];
}
};