概率-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的更多相关文章
- [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 ...
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 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] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- 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
题目如下: 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
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
随机推荐
- 简单的java开源图床
出于自用的目的,又找不到Java写的开源的程序,然后使用开源上传组件bootstrap-fileinput用 Spring Boot 写了一个图床 目前支持:七牛云,阿里云 GitHub: https ...
- Docker 运行容器 CentOS7 使用systemctl 启动报错 Failed to get D-Bus connection: Operation not permitted
原系统:Centos 7 Docker 版本:1.12.6 操作:安装并运行 Tomcat 问题:在创建好容器之后,并且进入系统运行启动tomcat [root@cd11558d3a22 /]# sy ...
- c++获取屏幕大小
API: 要取得屏幕大小,可以用下面几个函数: # include <windows.h>int cx = GetSystemMetrics( SM_CXFULLSCREEN ); int ...
- Mysql8以上需要指定时区serverTimezone
JDBC连接Mysql8以下 com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&charac ...
- 玩转 React(五)- 组件的内部状态和生命周期
文章标题总算是可以正常一点了-- 通过之前的文章我们已经知道:在 React 体系中所谓的 "在 JavaScript 中编写 HTML 代码" 指的是 React 扩展了 Jav ...
- elasticsearch 单机安装
一.elasticsearch下载 1.elastic 官网:https://www.elastic.co/cn/ 2.elasticsearch 下载地址:https://www.elastic.c ...
- seo搜索优化教程12-网站SEO诊断
为了使大家更方便的了解及学习网络营销推广.seo搜索优化,星辉信息科技强势推出seo搜索优化教程.此为seo教程第12课 行业分析 在搜索引擎中检索自己的站点,在检索结果及相关网站中分析自己在行业内的 ...
- duid 配置监控
web.xml中加入 <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class& ...
- java开发——Cloneable接口、clone()方法和深浅拷贝
1.实现Cloneable接口表明该类的对象是允许克隆的. 2.允许克隆的意思是:可以调用clone()方法. 3.深拷贝还是浅拷贝,取决于如何重写Object的clone()方法. 4.原对象和克隆 ...
- Ctrl+F5和F5区别
F5刷新的内容是从本地缓存中读取刷新,刷新本地缓存 Ctrl+F5直接读取服务器上的最新的内容—— Ctrl+F5会把Internet 临时文件夹的文件删除再重新从服务器下载,也就是彻底刷新页面了.. ...