# LeetCode: 999. 车的可用捕获量¶

## 1、题目描述¶

输入：[[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]



输入：[[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","p",".",".",".","."],
["p","p",".","R",".","p","B","."],
[".",".",".",".",".",".",".","."],
[".",".",".","B",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."]]



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

• $board[i][j]$ 可以是 'R'，'.'，'B' 或 'p'

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

## 2、解题思路¶

• 找到车(rook)的位置，然后上下左右扫描
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
R, C = len(board), len(board[0])

rook_row = 0
rook_col = 0

for r, row in enumerate(board):
for c, pos in enumerate(row):
if pos == "R":
rook_row = r
rook_col = c

count = 0

# right
for i in range(rook_row + 1, R):
if board[i][rook_col] == "B":
break
elif board[i][rook_col] == "p":
count += 1
break
# left
for i in range(rook_row, -1, -1):
if board[i][rook_col] == "B":
break
elif board[i][rook_col] == "p":
count += 1
break
# up
for i in range(rook_col + 1, C):
if board[rook_row][i] == "B":
break
elif board[rook_row][i] == "p":
count += 1
break
# down
for i in range(rook_col - 1, -1, -1):
if board[rook_row][i] == "B":
break
elif board[rook_row][i] == "p":
count += 1
break

return count