文章作者:Tyan 博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
**解析:**Version 1,先找到R
,再分别统计四个方向上的p
。
- Version 1
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
length = 8
x = -1
y = -1
for i in range(length):
for j in range(length):
if board[i][j] == 'R':
x = i
y = j
break
if x != -1:
break
count = 0
row = board[x]
column = [board[i][y] for i in range(length)]
count = self.find(row, y-1, y 1, length)
count = self.find(column, x-1, x 1, length)
return count
def find(self, arr, i, j, length):
count = 0
while i > -1:
if arr[i] == 'p':
count = 1
break
elif arr[i] == 'B':
break
i -= 1
while j < length:
if arr[j] == 'p':
count = 1
break
elif arr[j] == 'B':
break
j = 1
return count
Reference
- https://leetcode.com/problems/available-captures-for-rook/