Description
给定
补充国际象棋中马的移动方式,见上图
Constraints
Solution
可以定义一个
设
Code
class Solution {
public:
double knightProbability(int n, int k, int row, int column) {
double dp[101][26][26] {};
for(auto &vec : dp[0]) for(auto &v : vec) v = 1;
const int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
const int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
for(int step = 1; step <= k; ++step) {
for(int x = 0; x < n; ++x) {
for(int y = 0; y < n; ++y) {
for(int d = 0; d < 8; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if(nx < 0 || nx >= n) continue;
if(ny < 0 || ny >= n) continue;
dp[step][x][y] += 1.0/8 * dp[step-1][nx][ny];
}
}
}
}
return dp[k][row][column];
}
};