Leetcode 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 for i in range(8): for j in range(8): if board[i][j] == 'R': rook[0], rook[1] = i, j for k in range(rook[1], 0, -1): # left if board[rook[0]][k] == 'p': ans += 1 break elif board[rook[0]][k] == 'B': break for k in range(rook[1], 8): # right if board[rook[0]][k] == 'p': ans += 1 break elif board[rook[0]][k] == 'B': break for k in range(rook[0], 0, -1): # up if board[k][rook[1]] == 'p': ans += 1 break elif board[k][rook[1]] == 'B': break for k in range(rook[1], 8): # bottom if board[k][rook[1]] == 'p': ans += 1 break elif board[k][rook[1]] == 'B': break return ans