作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/knight-probability-in-chessboard/description/

题目描述:

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:

  1. N will be between 1 and 25.
  2. K will be between 0 and 100.
  3. The knight always initially starts on the board.

题目大意

有个N * N的棋盘,上面有个马,马走日字象走田嘛,找出这个马走了K步之后依然在这个棋盘上的概率。

解题方法

如果dfs的话一定会超时的,所以还是得用dp来解。

这个dp数组代表在某一轮中,这个马能到这个位置的次数。

dp更新的策略是,我们遍历棋盘的每个位置,当前的数值是能走到这个位置的在上一轮dp的数值 + 1。

这个题的对称性让这个题变得简单而又有趣。最内层的for循环对dp进行更新的时候是不用考虑索引位置的,因为对称性太美了。

时间复杂度是O(K * N ^ 2),空间复杂度是O(N ^ 2)。

注意,python2里面的/默认的是地板除,需要用float再除得到概率。

代码如下:

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 = [[0 for i in range(N)] for j in range(N)]
dp[r][c] = 1
directions = [(1, 2), (1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1), (-1, 2), (-1, -2)]
for k in range(K):
new_dp = [[0 for i in range(N)] for j in range(N)]
for i in range(N):
for j in range(N):
for d in directions:
x, y = i + d[0], j + d[1]
if x < 0 or x >= N or y < 0 or y >= N:
continue
new_dp[i][j] += dp[x][y]
dp = new_dp
return sum(map(sum, dp)) / float(8 ** K)

参考资料:

https://www.youtube.com/watch?v=MyJvMydR2G4

日期

2018 年 9 月 17 日 —— 早上很凉,夜里更凉

【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)的更多相关文章

  1. LeetCode 688. Knight Probability in Chessboard

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

  2. LeetCode——688. Knight Probability in Chessboard

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

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

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

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

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

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

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

随机推荐

  1. excel-大于0的数值标记红色且标记红色上箭头,小于0的数值标记绿色且标记绿色下箭头,等于0的数值标记黄色且标记右箭头

    0.数值是常规的数值: [蓝色]"↑"0;[红色]"↓"0;[黄色]"→"0 [蓝色]"↑"0.0;[红色]" ...

  2. Spark检查点机制

    Spark中对于数据的保存除了持久化操作之外,还提供了一种检查点的机制,检查点(本质是通过将RDD写入Disk做检查点)是为了通过lineage(血统)做容错的辅助,lineage过长会造成容错成本过 ...

  3. Https原理及证书管理

    Https原理及证书管理 SSL(Secure Sockets Layer,安全套接层)/TLS(Transport Layer Security,传输层安全)保证了客户端web服务器的连接安全.客户 ...

  4. How does “void *” differ in C and C++?

    C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; th ...

  5. SpringMVC responseBody注解分析

    @responsebody表示该方法的返回结果直接写入HTTP response body中一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@respo ...

  6. 对于HTML和XML的理解

    1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...

  7. Redis-5种基础数据结构

    Redis基础数据结构 知识整理源于<Redis深度历险 核心原理与应用实践>这本书 Redis 有的数据结构都以 唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应的 ...

  8. 使用JDBCTemplate执行DQL/DML语句

    package cn.itcast.datasource.jdbctemplate;import cn.itcast.domain.User;import cn.itcast.utils.JDBCUt ...

  9. 【C/C++】例题3-6 环状序列/算法竞赛入门经典/数组和字符串

    [字典序比较] 对于两个字符串,比较字典序,从第一个开始,如果有两位不一样的出现,那么哪个的ASCII码小,就是字典序较小.如果都一样,那么短的小. [题目] 输入一个环状串,输出最小的字典序序列. ...

  10. 【kafka学习笔记】kafka的基本概念

    在了解了背景知识后,我们来整体看一下kafka的基本概念,这里不做深入讲解,只是初步了解一下. kafka的消息架构 注意这里不是设计的架构,只是为了方便理解,脑补的三层架构.从代码的实现来看,kaf ...