为有牺牲多壮志,敢教日月换新天。

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2019-03-09 15:02  为敢技术  阅读(251)  评论(0编辑  收藏  举报