Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/74d691f2afe6783fd096dfe23d794052 to your computer and use it in GitHub Desktop.
Save jianminchen/74d691f2afe6783fd096dfe23d794052 to your computer and use it in GitHub Desktop.
Leetcode 688 - Knight probability in chessboard - study blog, my favorite analysis and code
/*
Leetcode 688: Knight probability in Chessboard
I like to study the blog and plan to study 30 minutes. First thing is to go over analysis in Chinese and make
analysis more readable and fit for my reading style.
http://www.cnblogs.com/grandyang/p/7639153.html
这道题给了我们一个大小为 N x N 国际象棋棋盘,上面有个骑士,相当于我们中国象棋中的马,能走‘日’字,给了我们一个起始位置,
然后说允许我们走 K 步,问走完 K 步之后还能留在棋盘上的概率是多少。那么要求概率,我们必须要先分别求出分子和分母,其中
分子是走完 K 步还在棋盘上的走法,分母是没有限制条件的总共的走法。那么分母最好算,每步走有 8 种跳法,那么 K 步就是 8
的 K 次方种了。
***Timeout concern ***
关键是要求出分子,一种方法是以给定位置为起始点,然后进行BFS,每步遍历8种情况,遇到在棋盘上的就计数器加 1,结果TLE了。
***Optimal solution ***
上论坛看大家的解法,结果发现都是换了一个角度来解决问题的,并不很关心骑士的起始位置,而是把棋盘上所有位置上经过K步还留
在棋盘上的走法总和都算出来,那么最后直接返回需要的值即可。
The algorithm is related to leetcode 576: Out of Boundary Paths。
*** implementation detail ***
Dynamic programming algorithm,我们可以用三维 DP 数组,也可以用二维 DP 数组来做,这里为了省空间,我们就用二维DP数组来做,
其中 dp[i][j] 表示在棋盘 (i, j) 位置上走完当前步骤还留在棋盘上的走法总和,初始化为 1,我们其实将步骤这个维度当成了时间维度
在不停更新。好,下面我们先写出 8 种‘日’字走法的位置的坐标,就像之前遍历迷宫上下左右四个方向坐标一样,这不过这次位置变了而已。
然后我们一步一步来遍历,每一步都需要完整遍历一遍棋盘的每个位置,新建一个临时数组 t,大小和 dp 数组相同,但是初始化为 0,
然后对于遍历到的棋盘上的每一个格子,我们都遍历 8 中解法,如果新的位置不在棋盘上了,直接跳过,否则就加上上一步中的 dp 数组
中对应的值,遍历完棋盘后,将 dp 数组更新为这个临时数组 t,参见代码如下:
*/
class Solution1 {
public:
double knightProbability(int N, int K, int r, int c) {
if (K == 0)
return 1;
vector<vector<double>> dp(N, vector<double>(N, 1));
vector<vector<int>> dirs{{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
for (int m = 0; m < K; ++m) {
vector<vector<double>> t(N, vector<double>(N, 0));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (auto dir : dirs) {
int x = i + dir[0], y = j + dir[1];
if (x < 0 || x >= N || y < 0 || y >= N)
continue;
t[i][j] += dp[x][y];
}
}
}
dp = t;
}
return dp[r][c] / pow(8, K);
}
};
/*
我们也可以使用有 memo 数组优化的递归来做,避免重复运算,从而能通过OJ。递归下的memo数组其实就是迭代下的dp数组,
这里我们用三维的数组,初始化为 0。在递归函数中,如果 k 为 0 了,说明已经走了 k 步,返回 1。如果memo[k][r][c]不为0,
说明这种情况之前已经计算过,直接返回。然后遍历8种走法,计算新的位置,如果不在棋盘上就跳过;然后更新memo[k][r][c],
使其加上对新位置调用递归的返回值,注意此时带入 k - 1 和新的位置,退出循环后返回 memo[k][r][c] 即可,参见代码如下:
*/
class Solution {
public:
vector<vector<int>> dirs{{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
double knightProbability(int N, int K, int r, int c) {
vector<vector<vector<double>>> memo(K + 1, vector<vector<double>>(N, vector<double>(N, 0.0)));
return helper(memo, N, K, r, c) / pow(8, K);
}
double helper(vector<vector<vector<double>>>& memo, int N, int k, int r, int c) {
if (k == 0)
return 1.0;
if (memo[k][r][c] != 0.0)
return memo[k][r][c];
for (auto dir : dirs) {
int x = r + dir[0], y = c + dir[1];
if (x < 0 || x >= N || y < 0 || y >= N) continue;
memo[k][r][c] += helper(memo, N, k - 1, x, y);
}
return memo[k][r][c];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment