概率-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, ...
随机推荐
- Redis从出门到高可用--Redis复制原理与优化
Redis从出门到高可用–Redis复制原理与优化 单机有什么问题? 1.单机故障; 2.单机容量有瓶颈 3.单机有QPS瓶颈 主从复制:主机数据更新后根据配置和策略,自动同步到备机的master/s ...
- 每天一个linux命令(15)-tail
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新, ...
- Web 项目刚要打包,却找不到项目资源?
编程无小事,不管是语言层面还是工具层面,都要熟悉,方能在编程中过程中众享丝滑,不然就随处卡顿,耗费时间不说,还没有任何成就感.撸码过程中用 Idea 也很多年了,工具或环境遇到问题,问下度娘就完事了, ...
- Hadoop环境搭建问题总结
最近抽空搭建了Hadoop完全分布式环境,期间遇到了很多问题,大部分问题还是可以在网上搜到的,这里说下自己遇到的两个没有找到结果的问题吧. 1.启动时报:没有那个文件或目录 原因:三台机器的用户名不一 ...
- 远程终端协议 TELNET
远程终端协议 TELNET 1.1.概述 TELNET 是一个简单的远程终端协议,也是因特网的正式标准. 用户用 TELNET 就可在其所在地通过 TCP 连接的23端口,使用主机名或 IP 地址登录 ...
- (原)人体姿态识别PyraNet
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/12424767.html 论文: Learning Feature Pyramids for Huma ...
- 基于abp框架的数据库种子数据初始化
目录 基于abp框架的数据库种子数据初始化 1.背景 2.参照 3.解决方案 3.1 初始化数据 3.2 依赖注入方法容器里获取数据库上下文 3.3 封装创建初始化数据列表方法 3.4 数据库中没有的 ...
- python大佬养成计划----HTML网页设计(序列)
序列化标签 1.有序标签--ol和li 有序列表标签是<ol>,是一个双标签.在每一个列表项目前要使用<li>标签.<ol>标签的形式是带有前后顺序之分的编号.如果 ...
- 解决Sprite Atlas打包Asset bundles时重复打包的问题
0x00 前言 在Unity 2018.4.6之前的版本,有一个和SpriteAtlas打AB包有关的常见问题.即当给Sprite Atlas打AB包时,Sprite Atlas Texture可能会 ...
- Layabox 世界坐标和屏幕坐标互转
最近在入坑Layabox,花了几天时间做世界坐标和屏幕坐标的互转,由于Layabox没有现成的代码所以只能自己手动写,大概就是模仿unity里面的ScreenToWorldPoint和WorldToS ...