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的更多相关文章

  1. [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 ...

  2. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

  3. 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 ...

  4. [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 ...

  5. LeetCode 688. Knight Probability in Chessboard

    原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...

  6. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...

  7. 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 ...

  8. 【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 ...

  9. LeetCode——688. Knight Probability in Chessboard

    一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...

随机推荐

  1. 修改android项目sdk版本

    1.右键单击项目--->properties---->Resource----->Android在Project Bulid Target对话框中选择你需要的Android版本.2. ...

  2. 从谷歌到脸书:为何巨头纷纷“钟情于”VR相机?

    VR的火爆,自然无需多言.而基于VR这一个概念,已经在多个相关行业不断衍生出新的产品.服务或内容.VR眼镜.VR头盔.VR相机.VR游戏.VR影视.VR应用--但VR产业的发展并不是齐头并进,而是出现 ...

  3. (转载)Eclipse中使用SVN

    为了方便个人使用,转载过来的,如需查阅,请前往原文地址:http://www.cnblogs.com/wvqusrtg/p/4993849.html 1.在Eclipse里下载Subclipse插件 ...

  4. C++程序设计--运算符重载

    目录 1, 运算符重载2, 赋值运算符的重载3, 流插入运算符重载4,自加/自减运算符的重载 运算符重载 作用:对抽象数据类型也能够直接使用C++提供的运算符.使得程序更简洁.代码更容易理解. 运算符 ...

  5. 对BFC的深层理解

    BFC(Block Formatting Context)块级格式化上下文 注意:BFC首先是块,其次需要具备下面的条件之一才可以(通俗来说,BFC就好比一所985或者211的高校,想要成为985或者 ...

  6. Python——7列表生成式

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  7. Channel Estimation for High Speed Wireless Systems using Gaussian Particle Filter and Auxiliary Particle Filter

    目录 论文来源 摘要 基本概念 1.时变信道 2.粒子滤波 3.高斯粒子滤波 4.辅助粒子滤波 比较 借鉴之处 论文来源 International Conference on Communicati ...

  8. python3.5以及scrapy,selenium,等 安装

    一.python3.5安装和配置 在安装的时候无意间发现了,python3.6没有给我自定义安装的机会,直接就C盘见:因此我选择了python3.5.<安装部分跳过,至于一条吃过痛苦的建议:不要 ...

  9. 十分钟复习CSS盒模型与BFC

    css盒模型与BFC 本文为收集整理总结网上资源 旨在系统复习css盒模型与bfc 节省复习时间 阅读10分钟 什么是盒模型 每一个文档中,每个元素都被表示为一个矩形的盒子,它都会具有内容区.padd ...

  10. js轮询及踩过的坑

    背景 下午四点,天气晴朗,阳光明媚,等着下班产品:我希望页面上的这个数据实时变化开发:···,可以,用那个叫着WebSocket的东西,再找一个封装好框架,如:mqtt(感觉自己好机智)产品:要开发好 ...