Knight Probability in Chessboard
2018-07-14 09:57:59
问题描述:
问题求解:
本题本质上是个挺模板的题目。本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性。这种计数的问题可以使用动态规划来进行解决。
在本题中有两个注意点:
1)可以使用两个数组滚动使用来实现重复利用,这里我的实现使用了一个trick就是结合奇偶性来完成数组滚动;
2)dp数组需要定义成double类型的,如果定义成int类型的,在后期会出现溢出的问题。
public double knightProbability(int N, int K, int r, int c) {
double[][][] dp = new double[2][N][N];
int[][] dir = new int[][]{
{-1, -2},
{-2, -1},
{1, -2},
{2, -1},
{-1, 2},
{-2, 1},
{1, 2},
{2, 1},
};
dp[0][r][c] = 1;
for (int k = 0; k < K; k++) {
fill2D(dp, (k + 1) & 1, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int m = 0; m < 8; m++) {
int u = i + dir[m][0];
int v = j + dir[m][1];
if (u < 0 || u >= N || v < 0 || v >= N) continue;
dp[(k + 1) & 1][u][v] += dp[k & 1][i][j];
}
}
}
}
double total = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
total += dp[K & 1][i][j];
}
}
return total / Math.pow(8, K);
} private void fill2D(double[][][] array, int layer, int n) {
for (int i = 0; i < n; i++) Arrays.fill(array[layer][i], 0);
}
Follow up:
问题描述:
问题求解:
如出一辙。
public int findPaths(int m, int n, int N, int i, int j) {
int[][] dp = new int[m][n];
dp[i][j] = 1;
int res = 0;
int mod = (int)Math.pow(10, 9) + 7;
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int step = 0; step < N; step++) {
int[][] cur = new int[m][n];
for (int pi = 0; pi < m; pi++) {
for (int pj = 0; pj < n; pj++) {
for (int[] dir : dirs) {
int x = pi + dir[0];
int y = pj + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n) {
res = (res + dp[pi][pj]) % mod;
}
else cur[x][y] = (cur[x][y] + dp[pi][pj]) % mod;
}
}
}
dp = cur;
}
return res;
}
Knight Probability in Chessboard的更多相关文章
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- [LeetCode] 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 ...
- [Swift]LeetCode688. “马”在棋盘上的概率 | 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 ...
- 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 exa ...
- 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 ...
- LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...
- 【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 exa ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- LeetCode——688. Knight Probability in Chessboard
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
随机推荐
- windows分区
- Object-C-Foundation-NSDate
NSDate 表达日期表达时间的方法 NSDate *now=[NSDate date]; 获得当前日期 NSDate *tomrrow=[now dateByAddingTimeInterval:2 ...
- Linux服务器配置---安装vsftpd
安装vsftpd 大多数Linux系统都使用vsftpd,因此这里我们也安装vsftpd 1.安装vsftpd [root@localhost phpMyAdmin]# yum install -y ...
- ES6学习笔记之map、set与数组、对象的对比
ES6 ES5中的数据结构,主要是用Array和Object.在ES6中主要新增了Set和Map数据结构.到目前为止,常用的数据结构有四种Array.Object.Set.Map.下面话不多说了,来一 ...
- tomcat 9.0.4 性能调优
参考了网上的一些优化参数,但是在启动中发现 有2个报错: 11-Feb-2018 15:57:23.293 警告 [main] org.apache.catalina.startup.SetAllPr ...
- P2260 [清华集训2012]模积和
P2260 [清华集训2012]模积和 整除分块+逆元 详细题解移步P2260题解板块 式子可以拆开分别求解,具体见题解 这里主要讲的是整除分块(数论分块)和mod不为素数时如何求逆元 整除分块:求Σ ...
- 浏览器内核、排版引擎、js引擎
[定义] 浏览器最重要或者说核心的部分是“Rendering Engine”,可大概译为“渲染引擎”,不过我们一般习惯将之称为“浏览器内核”.负责对网页语法的解释(如标准通用标记语 言下的一个应用HT ...
- 20145212罗天晨 逆向及Bof基础实践
20145212罗天晨<网络对抗>第1周学习总结--逆向及Bof基础实践 逆向及Bof基础实践 一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代 ...
- 关键字union
union有一个作用就是判断,pc是大端存储还是小端存储的,x86是小端存储的,这个东西是有cpu决定的.arm(由存储器控制器决定)和x86一样都是小端的. 下面的是一个大端小端的一个例子,代码如下 ...
- vs2012添加自定义资源步骤
1.新建Win32工程 2.选中Resource Files -> add resource 点击打开 这里需要你填写一个Resource Type ,假如你之前已经有了,就直接输入,没有的话在 ...