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. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

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

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

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

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

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

  6. LeetCode 688. Knight Probability in Chessboard

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

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

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

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

  9. LeetCode——688. Knight Probability in Chessboard

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

随机推荐

  1. uva10003

    /* 2014.3.6 这题说的是给你了一根木棒 然后 n 个点(线段上的点) 然后计算 在这 n个点上都切下去的 最小花费 举个例子 100 3 25 50 75 如果 从 25 开始切 然后切 5 ...

  2. PT100高精度测温电路 AD623+REF3030(转)

    源: PT100高精度测温电路 AD623+REF3030(很稳定)

  3. python中hasattr, getattr,setattr及delattr四个方法

    通过一个实例来说明,这四个函数的用法: 首先一个如下的一个简单的类: class Animal(object): def __init__(self,name, zone): self.name = ...

  4. python模块-random随机数模块

    导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生成[a,b]之间的随机整数 3.random. ...

  5. 20145334赵文豪《网络对抗》-逆向及Bof基础实践

    本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShell,会返 ...

  6. 20165310_获奖感想与Java阶段性学习总结

    获奖感想与Java阶段性学习总结 一.Learning By Doing ​ 在此之前,其实我并没有想到能够成为小黄杉的第一批成员之一,喜悦之余,也感受到了许多的压力.小黄杉一方面代表了老师对于我这一 ...

  7. UNIX系统的显示时间何时会到尽头

    本文转载自:http://www.cnblogs.com/dfcao/p/expertCprogramming_intr0.html 本文分为三个小块: 一.UNIX系统中时间的存储形式: 二. ti ...

  8. Python跨平台打包

    对于pyinstaller,可以完成在windows,linux,和mac下的python脚本编译,生成exe,elf,.app文件: 1.使用方法: 在pyinstaller的官网上下载,一般是源码 ...

  9. Java8中数据流的使用

    Code: @Data @ToString @NoArgsConstructor @AllArgsConstructor public class Employee { private Integer ...

  10. IHttpHandler IHttpModule

    ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...