Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-07-22 22:13:16
https://leetcode.com/problems/knight-probability-in-chessboard/description/
688. Knight Probability in Chessboard
給你一個數字 n 表示一個 n x n 的西洋棋盤,他的座標用 0-index 來表示
我們把一個騎士放在棋盤的 [row, column] 位置,騎士可以往八個方向移動
https://assets.leetcode.com/uploads/2018/10/12/knight.png
棋子在移動 k 步時停下(可以走出棋盤外),求出騎士恰好移動 k 步之後,他仍留
在棋盤上的機率是多少。
Example 1:
Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
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.
Example 2:
Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000
方法一:DFS(TLE)
1.一個棋子可以走 k 步,每次有八種走法,所以共有 8^k 種走法,我們只要求出
哪些走法可以停在棋盤上並放在分子即可。
2.用DFS模擬棋子的所有走法,當恰好走完 k 步時沒越界, res 遞增。
3.答案為 res / 所有走法。
Java Code:
作者: a9486l (a9486l)   2023-07-22 22:14:00
大師
作者: ILoveErr (英梨梨我老婆)   2023-07-22 22:14:00
大師
作者: uiojkl789 (雪!花!喵!喵!)   2023-07-22 22:14:00
怎麼還沒新增到簽名檔
作者: JerryChungYC (JerryChung)   2023-07-22 22:16:00
大師
作者: Che31128 (justjoke)   2023-07-22 22:22:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com