# Leetcode 999.车的可用捕获量

## 1 题目描述（Leetcode题目链接）

在一个 8 x 8 的棋盘上，有一个白色车（rook）。也可能有空方块，白色的象（bishop）和黑色的卒（pawn）。它们分别以字符 “R”，“.”，“B” 和 “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 题解

题目很简单，先找到车的位置再向四个方向走一下，看能不能吃到卒。

class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
di = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
pos = [i, j]
retv = 0
for d in di:
x, y = pos[0]+d[0], pos[1]+d[1]
while x >= 0 and x < 8 and y >= 0 and y < 8:
if board[x][y] == 'p':
retv += 1
break
if board[x][y] == 'B':
break
x, y = x+d[0], y+d[1]
return retv


©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie