亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

leetcode中如何解決車的可用捕獲量問題

發布時間:2022-01-05 16:22:43 來源:億速云 閱讀:143 作者:小新 欄目:大數據

小編給大家分享一下leetcode中如何解決車的可用捕獲量問題,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

車的可用捕獲量


在一個 8 x 8 的棋盤上,有一個白色車(rook)。也可能有空方塊,白色的象(bishop)和黑色的卒(pawn)。它們分別以字符 “R”,“.”,“B” 和 “p” 給出。大寫字符表示白棋,小寫字符表示黑棋。


車按國際象棋中的規則移動:它選擇四個基本方向中的一個(北,東,西和南),然后朝那個方向移動,直到它選擇停止、到達棋盤的邊緣或移動到同一方格來捕獲該方格上顏色相反的卒。另外,車不能與其他友方(白色)象進入同一個方格。


返回車能夠在一次移動中捕獲到的卒的數量。


示例 1:


leetcode中如何解決車的可用捕獲量問題


輸入:

[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

輸出:3

解釋:

在本例中,車能夠捕獲所有的卒。


示例 2:

leetcode中如何解決車的可用捕獲量問題

輸入:

[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

輸出:0

解釋:

象阻止了車捕獲任何卒。


示例 3:

leetcode中如何解決車的可用捕獲量問題

輸入:

[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]

輸出:3

解釋: 

車可以捕獲位置 b5,d6 和 f5 的卒。



提示:

board.length == board[i].length == 8

board[i][j] 可以是 'R','.','B' 或 'p'

只有一個格子上存在 board[i][j] == 'R'



思路:

  • 題目的意思是中間R走一下能吃p的次數,方向是上下左右四個方向

  • 限制條件是走一次,以及遇到B就表明該方向不通

class Solution:    def numRookCaptures(self, board: List[List[str]]) -> int:        cnt, st, ed = 0, 0, 0        # 方向數組        direction = [(0, 1), (0, -1), (1, 0), (-1, 0)]        # 找到R        for i in range(8):            for j in range(8):                if board[i][j] == 'R':                    st = i                    ed = j        # 朝著四個方向探索        for i in range(4):            step = 0            while True:                dx, dy = direction[i]                tx = st + step * dx                ty = ed + step * dy                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == 'B':                    break                if board[tx][ty] == 'p':                    cnt += 1                    break                 step += 1  #每次移動的范圍,第一次移動一格,找不到的話移動兩格        return cnt

看完了這篇文章,相信你對“leetcode中如何解決車的可用捕獲量問題”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

西峡县| 屯留县| 格尔木市| 桃园县| 东辽县| 黑山县| 新乡市| 繁峙县| 黄陵县| 德江县| 四子王旗| 务川| 胶南市| 大城县| 淅川县| 洛南县| 治县。| 中卫市| 永吉县| 瑞昌市| 疏附县| 天祝| 龙里县| 大连市| 商丘市| 大新县| 赫章县| 滦南县| 曲沃县| 前郭尔| 祁连县| 牙克石市| 锡林浩特市| 芦溪县| 巴林左旗| 长海县| 富平县| 耒阳市| 临西县| 裕民县| 醴陵市|