【leetcode】688. Knight Probability in Chessboard
题目如下:
On an
N
xN
chessboard, a knight starts at ther
-th row andc
-th column and attempts to make exactlyK
moves. The rows and columns are 0 indexed, so the top-left square is(0, 0)
, and the bottom-right square is(N-1, N-1)
.A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
K
moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.Example:
Input: 3, 2, 0, 0
Output: 0.0625
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.Note:
N
will be between 1 and 25.K
will be between 0 and 100.- The knight always initially starts on the board.
解题思路:本题和【leetcode】576. Out of Boundary Paths 非常相似,都是动态规划的方法。最大的不同在于本题有八个方向。主要代码几乎完全复用【leetcode】576. Out of Boundary Paths。
代码如下:
class Solution(object):
def knightProbability(self, N, K, r, c):
"""
:type N: int
:type K: int
:type r: int
:type c: int
:rtype: float
"""
dp = []
for i in range(K+1):
tl = []
for j in range(N):
tl.append([0] * N)
dp.append(tl)
dp[0][r][c] = 1
direction = [(-2,1),(-1,2),(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1)]
count = 0
for x in range(1,len(dp)):
for y in range(len(dp[x])):
for z in range(len(dp[x][y])):
for (ny, nz) in direction:
if (y + ny) >= 0 and (y + ny) < N and (z + nz) >= 0 and (z + nz) < N:
dp[x][y][z] += dp[x - 1][y + ny][z + nz]
for i in (dp[-1]):
for j in i:
count += j
return float(count) / float(pow(8,K))
【leetcode】688. Knight Probability in Chessboard的更多相关文章
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 【LeetCode】935. Knight Dialer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...
- 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
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
- 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】935. Knight Dialer
题目如下: A chess knight can move as indicated in the chess diagram below: . This time, we p ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- hdu 5868:Different Circle Permutation 【Polya计数】
似乎是比较基础的一道用到polya定理的题,为了这道题扣了半天组合数学和数论. 等价的题意:可以当成是给正n边形的顶点染色,旋转同构,两种颜色,假设是红蓝,相邻顶点不能同时为蓝. 大概思路:在不考虑旋 ...
- 多次读取HttpEntity内容
有时,需要重复读取HttpEntity,直接使用是行不通的,这时需要使用BufferedHttpEntity类将其进行包装一下. public static void main(String[] ar ...
- delphi 异形窗体可半透明
unit xDrawForm; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, Menus, Graphic ...
- LG2704 [NOI2001] 炮兵阵地
题目描述 (试题来源:Link ) 司令部的将军们打算在 \(N\times M\) 的网格地图上部署他们的炮兵部队.一个 \(N\times M\) 的地图由 \(N\) 行 \(M\) 列组成,地 ...
- 建议66,67 注意Arrays.asList()的使用
代码 public static void main(String[] args) { int[]data = {1,2,3,4,5}; List list = Arrays.asList(data) ...
- Java学习之Thread方法
一.sleep()方法:在同步中,释放CPU执行权,不释放同步锁,意味着程序停止 二.停止线程方法:原理:run执行完成 1.设置标记 class StopThread implements Runn ...
- 家用NAS配置方案
对家用用户而言,NAS即一台下载机,硬件需要满足以下几点: 1.稳定性:24×7稳定无故障运行. 2.拓展性:较多的硬盘槽位,便于容量扩容: 3.体积小巧:占地面积小,便于放置. 4.方便远程管理:无 ...
- [Flask] 异步非阻塞IO实现
Flask默认是不支持非阻塞IO的,表现为: 当 请求1未完成之前,请求2是需要等待处理状态,效率非常低. 在flask中非阻塞实现可以由2种: 启用flask多线程机制 # Flask from f ...
- 《单词的减法》state1~state17(第二遍学习记录)
单词的减法(二) 2016.05.18.2016.05.21 state 1 advisory 顾问的,劝告的 anticipate/participate 期望/参加 state 2 applian ...
- 斯坦福【概率与统计】课程笔记(六):EDA | 标准差和方差
这一篇比较简单,就不展开记录了,方差和标准差的计算方法记住了就可以. 计算mean 计算每个样本与mean的差值的平方,将其累加后除以(样本数-1)[注:这里的除数可以是n-1也可以是n],即得到方差 ...