【leetcode】688. Knight Probability in Chessboard
题目如下:
On an
NxNchessboard, a knight starts at ther-th row andc-th column and attempts to make exactlyKmoves. 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
Kmoves 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:
Nwill be between 1 and 25.Kwill 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 ...
随机推荐
- HTTP协议-Cookie和Session详解
前言: 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的跟踪技术就是Cookie和Session. Cookie通过在客户端记录信息确定用户身份,Session通过在 ...
- cocos2D-X 打包
{ //首先有java jdk,android sdk,android ndk //用android studio import //匹配gradle 的版本 有些gradle可能下载不下来,不用慌 ...
- Redis5离线安装
1. 直接上redis官网安装包, 然后上传服务器 https://redis.io/download 2. 解压 tar -zxvf redis-5.0.6.tar.gz 3. 进入redis根目标 ...
- 基于oracle 的PL/SQL编程 - 存储过程
接上篇,游标使用的语句,相当于一段匿名的函数,窗口关闭了就不存在了.如果想要窗口关闭了,还能继续执行那段代码,就需要存储过程了: PLSQL是指一个个PLSQL的业务处理过程存储起来进行复用,这些被存 ...
- 【Linux】关闭selinux
vi /etc/selinux/config 将SELINUX=enforcing改为SELINUX=disabled 设置后需要重启才能生效
- Centos7.4 配置之MySQL 8.0【转】
首先查看Mysql最新版本, 此时,目前最新版本为8.0. 开始安装前需要一些准备工作. 1,将本地的MariaDB或者已经安装的MySQL其他版本卸载. (一)卸载本地的本地的MariaDB: 由于 ...
- VMware中对Linux虚拟机的网络配置静态IP的配置
前言 踏出象牙塔,进入公司,由于公司的所有产品都是Linux下的,必然自己这段时间需要在自己的工作机器先学习一下.项目代码是用Source Insight进行查看的,总是Ctrl + Alt的切来切去 ...
- JVM调优(四)——tomcat远程debug
JVM调优(四)--tomcat远程debug tomcat远程debug jdwp协议 使用步骤 登录远程服务器,进入tomcat目录,并打开文件: //tomcat/bin/startup.sh ...
- TSV 与 CSV
TSV : Tab-separated values 用制表符分隔值. CSV : Comma-separated values 用逗号分隔值. 参考 RFC 4180 - Common Format ...
- 110、TensorFlow张量值的计算
import tensorflow as tf #placeholders在没有提供具体值的时候不能使用eval方法来计算它的值 # 另外的建模方法可能会使得模型变得复杂 # TensorFlow 不 ...
