-
Notifications
You must be signed in to change notification settings - Fork 3
/
688.cpp
19 lines (19 loc) · 880 Bytes
/
688.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<pair<int, int>> directions = {{1, 2}, {2, 1}, {-1, 2}, {-2, 1}, {1, -2}, {2, -1}, {-1, -2}, {-2, -1}};
double dp(vector<vector<vector<double>>>& memo, int n, int row, int col, int k) {
if (k == 0) return memo[row][col][k] = 1.0;
if (memo[row][col][k] != 0) return memo[row][col][k];
for (auto direction : directions) {
int _x = row + direction.first;
int _y = col + direction.second;
if (_x < 0 || _x >= n || _y < 0 || _y >= n) continue;
memo[row][col][k] += dp(memo, n, _x, _y, k - 1);
}
return memo[row][col][k];
}
double knightProbability(int n, int k, int row, int column) {
vector<vector<vector<double>>> memo(n, vector<vector<double>>(n, vector<double>(k + 1, 0)));
return dp(memo, n, row, column, k) / pow(8, k);
}
};