题目如下:

On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K 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的更多相关文章

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

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

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

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

  3. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  4. LeetCode 688. Knight Probability in Chessboard

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

  5. LeetCode——688. Knight Probability in Chessboard

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

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

  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】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. leetcode-167周赛-1293-网格中的最短路径

    题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...

  2. intellij IDEA启动springboot项目报无效的源发行版错误解决方法

    从http://start.spring.io/ 上下载的springboot 模板项目,导入intellij 后,报如下错误,原因是intellij 默认使用的Java compiler 是1.8版 ...

  3. CALayer的mask属性

    可以对图层按path进行指定裁剪 //#import "ViewController.h" // //@interface ViewController () // //@end ...

  4. 怎么修改Xcode新项目或新文件最上面的Creat By XXX

    说明:这个名字是从当前电脑的管理员信息获取的,所以要去修改管理员个人信息. 呵呵 如果改了不行的话,就重启Xcode或者电脑试试吧.

  5. HTML基础知识笔记摘要

    HTML互联网三大基石:1.HTML:显示数据2.HTTP:传输数据 http传输协议3.URL:定位数据协议://ip地址或主机名:端口/网络中的内容... HTML(hyper text make ...

  6. C++ string.replace的使用

    //下面是一个检查一个字符串中是否有'.'的函数,该函数将找到的'.'转化为'_'. inline void checkName(string& name) { std::; while (s ...

  7. [CSP-S模拟测试]:God Knows(线段树维护单调栈)

    题目描述 小$w$来到天堂的门口,对着天堂的大门发呆.大门上有一个二分图,左边第$i$个点连到右边第$p_i$个点.(保证$p_i$是一个排列).小$w$每次可以找左边某个对应连线尚未被移除的点$i$ ...

  8. arm可以干什么

    ARM开发可以控制各种电机.arm性能很强 ,内存更大, c语言当然可以.ARM是32位的,单片机是8位的,运行速度快很多,最关键的是可以跑操作系统.控制部分的内容ARM当然可以胜任,而且ARM的资源 ...

  9. Red Gate .NET Reflector

    Debug and decompile inside Visual Studio (VSPro edition) Use the Visual Studio debugger Use your reg ...

  10. webservice 学习笔记 1

    Webservice----------->跨语言服务调用 (视频学习总结) 1-1.有OA系统 需要添加一个功能,登录之后显示天气情况 此时可以使用Webservice eg1: 气象局自己有 ...