zoukankan      html  css  js  c++  java
  • [Swift]LeetCode688. “马”在棋盘上的概率 | Knight Probability in Chessboard

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/10500839.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.

    已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始。即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1)。 

    现有一个 “马”(也译作 “骑士”)位于 (r, c) ,并打算进行 K 次移动。 

    如下图所示,国际象棋的 “马” 每一步先沿水平或垂直方向移动 2 个格子,然后向与之相垂直的方向再移动 1 个格子,共有 8 个可选的位置。

      

    现在 “马” 每一步都从可选的位置(包括棋盘外部的)中独立随机地选择一个进行移动,直到移动了 K 次或跳到了棋盘外面。

    求移动结束后,“马” 仍留在棋盘上的概率。 

    示例:

    输入: 3, 2, 0, 0
    输出: 0.0625
    解释: 
    输入的数据依次为 N, K, r, c
    第 1 步时,有且只有 2 种走法令 “马” 可以留在棋盘上(跳到(1,2)或(2,1))。对于以上的两种情况,各自在第2步均有且只有2种走法令 “马” 仍然留在棋盘上。
    所以 “马” 在结束后仍在棋盘上的概率为 0.0625。 

    注意:

    • N 的取值范围为 [1, 25]
    • K 的取值范围为 [0, 100]
    • 开始时,“马” 总是位于棋盘上

    Runtime: 108 ms
    Memory Usage: 19.4 MB
     1 class Solution {
     2     var dirs:[[Int]] = [[-1,-2],[-2,-1],[-2,1],[-1,2],[1,2],[2,1],[2,-1],[1,-2]]
     3     func knightProbability(_ N: Int, _ K: Int, _ r: Int, _ c: Int) -> Double {
     4         var memo:[[[Double]]] = [[[Double]]](repeating:[[Double]](repeating:[Double](repeating:0.0,count:N),count:N),count:K + 1)
     5         return helper(&memo, N, K, r, c) / pow(8.0, Double(K))
     6     }
     7     
     8     func helper(_ memo:inout [[[Double]]],_ N:Int,_ k:Int,_ r:Int,_ c:Int) -> Double
     9     {
    10         if k == 0 {return 1.0}
    11         if memo[k][r][c] != 0.0 {return memo[k][r][c]}
    12         for dir in dirs
    13         {
    14             var x:Int = r + dir[0]
    15             var y:Int = c + dir[1]
    16             if x < 0 || x >= N || y < 0 || y >= N
    17             {
    18                 continue
    19             }
    20             memo[k][r][c] += helper(&memo, N, k - 1, x, y)
    21         }
    22         return memo[k][r][c]
    23     }
    24 }
  • 相关阅读:
    Java代码实现WORD转PDF
    用Java实现在【520,1314】之间生成随机整数的故事
    solr 6.5.1 linux 环境安装
    并发编程学习笔记(15)----Executor框架的使用
    并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理
    并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理
    并发编程学习笔记(12)----Fork/Join框架
    并发编程学习笔记(11)----FutureTask的使用及实现
    并发编程学习笔记(10)----并发工具类CyclicBarrier、Semaphore和Exchanger类的使用和原理
    服务器被植入木马,CPU飙升200%
  • 原文地址:https://www.cnblogs.com/strengthen/p/10500839.html
Copyright © 2011-2022 走看看