leetcode 688. Knight Probability in Chessboard 题解
On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K 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:
Nwill be between 1 and 25.Kwill be between 0 and 100.- The knight always initially starts on the board.
思路:这道题目初看像一道深度优先搜索题,而且偏简单,但是dfs的代码显然是会超时的,重复计算还是太多了,于是也就只有使用动态规划了。
代码1:dfs方法TLE
class Solution { public: double knightProbability(int N, int K, int r, int c) { if(r > N-1 || c > N-1 || r<0 || c<0) return 0; if(K == 0) return 1; return (1.0/8*knightProbability(N,K-1,r+2,c+1) +1.0/8*knightProbability(N,K-1,r+2,c-1) +1.0/8*knightProbability(N,K-1,r-2,c+1) +1.0/8*knightProbability(N,K-1,r-2,c-1) +1.0/8*knightProbability(N,K-1,r+1,c+2) +1.0/8*knightProbability(N,K-1,r+1,c-2) +1.0/8*knightProbability(N,K-1,r-1,c+2) +1.0/8*knightProbability(N,K-1,r-1,c-2)); } };
代码2:使用动态规划,把每一格通过k步之后能够在格子里面的概率记录下来,时间复杂度和空间复杂度都是O(N*N*K)
class Solution { public: double knightProbability(int N, int K, int r, int c) { vector<vector<vector<double>>> dp(N,vector<vector<double>>(N,vector<double>(K+1,0))); vector<pair<int,int>> direct = {{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}}; for(int i = 0; i<N; ++i){ for(int j = 0; j<N; ++j){ dp[i][j][0] = 1; } } for(int i = 1; i<=K; ++i){ for(int m = 0; m<N;++m){ for(int n = 0; n<N;++n){ for(auto item:direct){ dp[m][n][i] += valid(m+item.first,n+item.second,N)?0.125*dp[m+item.first][n+item.second][i-1]:0; } } } } return dp[r][c][K]; } bool valid(int x,int y,int N){ return !(x > N-1 || y > N-1 || x<0 || y<0); } };
这道题直接把所有方向表示在一个vector里面简化了代码值得学习。

浙公网安备 33010602011771号