—Easy

https://leetcode.com/problems/available-captures-for-rook/Leetcode-999. Available Captures for Rook

 Code:

class Solution:
    def numRookCaptures(self, board) -> int:
        ans_cnt = 0
        m = len(board)
        n = len(board[0])
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'R':
                    for e in range(0,i)[::-1]:
                        if board[e][j] == 'p':
                            ans_cnt += 1
                            #print('is here :', e, j)
                            break
                        if board[e][j] == 'B':
                            break
                        if board[e][j] == '.':
                            continue
                    for e in range(i+1,m):
                        if board[e][j] == 'p':
                            ans_cnt += 1
                            #print('is here :', e, j)
                            break
                        if board[e][j] == 'B':
                            break
                        if board[e][j] == '.':
                            continue
                    for e in range(0,j)[::-1]:
                        if board[i][e] == 'p':
                            ans_cnt += 1
                            #print('is here :', i, e)
                            break
                        if board[i][e] == 'B':
                            break
                        if board[i][e] == '.':
                            continue
                    for e in range(j+1,n):
                        if board[i][e] == 'p':
                            ans_cnt += 1
                            #print('is here :',i,e)
                            break
                        if board[i][e] == 'B':
                            break
                        if board[i][e] == '.':
                            continue
                    break
        return ans_cnt

# s = Solution()
# print(s.numRookCaptures([[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]))
# print(s.numRookCaptures([[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]))
# print(s.numRookCaptures([[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]))

思路:

1.找到R的位置进行上下左右四个方向遍历即可

2.range的逆序

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2021-11-18
  • 2021-08-21
  • 2022-02-20
  • 2021-08-04
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2021-10-26
  • 2021-09-19
  • 2021-06-13
  • 2022-01-07
  • 2021-07-03
相关资源
相似解决方案