https://leetcode.com/problems/knight-probability-in-chessboard/
On an
N
xN
chessboard, a knight starts at ther
-th row andc
-th column and attempts to make exactlyK
moves. The rows and columns are 0 indexed, so the top-left square is(0, 0)
, and the bottom-right square is(N-1, N-1)
.A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
K
moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.Example:
Input: 3, 2, 0, 0 Output: 0.0625 Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625.Note:
N
will be between 1 and 25.K
will be between 0 and 100.- The knight always initially starts on the board.
向下扩散,记忆式DFS:
class Solution {
public:
vector<vector<vector<double>>> cnt;
vector<int> r_step = {-2, -1, 1, 2, 2, 1, -1, -2};
vector<int> c_step = {1, 2, 2, 1, -1, -2, -2, -1};
int bound;
double knightProbability(int N, int K, int r, int c) {
cnt = vector<vector<vector<double>>>(K+1, vector<vector<double>>(N, vector<double>(N, 0)));
bound = N;
dfs(K, r, c);
return cnt[K][r][c];
}
void dfs(int K, int r, int c){
if(cnt[K][r][c] != 0) return;
if(K == 0){
// total[K][r][c] = 1;
cnt[K][r][c] = 1;
}else{
int nxt_r, nxt_c;
for(int i = 0; i < 8; ++i){
nxt_r = r+r_step[i];
nxt_c = c+c_step[i];
if(!(nxt_r < 0 || nxt_r >= bound || nxt_c < 0 || nxt_c >= bound)){
dfs(K-1, nxt_r, nxt_c);
cnt[K][r][c] += cnt[K-1][nxt_r][nxt_c]/8.0;
}
}
}
}
};
向上聚拢,DP:
DP的特点就是从trivial的情况开始计算
在这一题中只需要关心上一步的情况,这个 cur = K%2, pre = 1-cur的方法很酷
class Solution {
public:
double knightProbability(int N, int K, int r, int c) {
vector<int> r_step = {-2, -1, 1, 2, 2, 1, -1, -2};
vector<int> c_step = {1, 2, 2, 1, -1, -2, -2, -1};
vector<vector<vector<double>>>arr(2, vector<vector<double>>(N, vector<double>(N, 0)));
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
arr[0][i][j] = 1.0;
}
}
for(int round = 1; round <= K; ++round){
int cur = round % 2;
int pre = 1-cur;
for(int rr = 0; rr < N; ++rr){
for(int cc = 0; cc < N; ++cc){
double tem = 0;
int pre_r, pre_c;
for(int i = 0; i < 8; ++i){
pre_r = rr + r_step[i];
pre_c = cc + c_step[i];
if(pre_r >= 0 && pre_r < N && pre_c >= 0 && pre_c < N) tem += arr[pre][pre_r][pre_c]/8;
}
arr[cur][rr][cc] = tem;
}
}
}
return arr[K%2][r][c];
}
};