Skip to content

Latest commit

 

History

History
176 lines (105 loc) · 4.55 KB

0999._Available_Captures_for_Rook.md

File metadata and controls

176 lines (105 loc) · 4.55 KB

999. Available Captures for Rook

难度: Easy

刷题内容

原题连接

内容描述

On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, and black pawns.  These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.

The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.  Also, rooks cannot move into the same square as other friendly bishops.

Return the number of pawns the rook can capture in one move.

 

Example 1:



Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: 
In this example the rook is able to capture all the pawns.
Example 2:



Input: [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0
Explanation: 
Bishops are blocking the rook to capture any pawn.
Example 3:



Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: 
The rook can capture the pawns at positions b5, d6 and f5.
 

Note:

board.length == board[i].length == 8
board[i][j] is either 'R', '.', 'B', or 'p'
There is exactly one cell with board[i][j] == 'R'

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(1)******

开始没看懂题目,总体来说就是有一个格子是R,然后这个R上下左右四个方向走,对于每个方向, 如果能够碰到一个p就吃到一个卒,碰到p之前如果碰到B就立马停下了

返回能够吃到卒的个数,最多4个

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        row = len(board)
        col = len(board[0])
        m,n = 0, 0
        for i in range(row):
            for j in range(col):
                if board[i][j] == 'R':
                    m,n = i, j
                    break
        res = 0      
        a,b = m,n
        while a > 0:
            if board[a-1][b] == 'p':
                res += 1
                break
            elif board[a-1][b] == '.':
                a = a-1
            elif board[a-1][b] == 'B':
                break
        a,b = m,n
        while a < row - 1:
            if board[a+1][b] == 'p':
                res += 1
                break
            elif board[a+1][b] == '.':
                a = a+1
            elif board[a+1][b] == 'B':
                break
        a,b = m,n
        while b > 0:
            if board[a][b-1] == 'p':
                res += 1
                break
            elif board[a][b-1] == '.':
                b = b-1
            elif board[a][b-1] == 'B':
                break
        a,b = m,n
        while b < col - 1:
            if board[a][b+1] == 'p':
                res += 1
                break
            elif board[a][b+1] == '.':
                b = b + 1
            elif board[a][b+1] == 'B':
                break
        return res
        

思路 2 - 时间复杂度: O(N)- 空间复杂度: O(1)******

更好地实现

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        for i in range(8):
            for j in range(8):
                if board[i][j] == 'R':
                    x0, y0 = i, j
        res = 0
        for i, j in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            x, y = x0 + i, y0 + j
            while 0 <= x < 8 and 0 <= y < 8:
                if board[x][y] == 'p': res += 1
                if board[x][y] != '.': break
                x, y = x + i, y + j
        return res