题目链接:
https://leetcode-cn.com/problems/available-captures-for-rook/
解题
定位车的坐标,为t=(x,y)
根据车的坐标,向上下左右遍历,遇到有棋子的格子就停下,判断
class Solution:
def numRookCaptures(self, board) -> int:
result = 0
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
x = i
y = j
break
for i in range(x+1,8):
if board[i][y] != '.':
if board[i][y] == 'p':
result += 1
break
for i in range(x-1,-1,-1):
if board[i][y] != '.':
if board[i][y] == 'p':
result += 1
break
for j in range(y+1,8):
if board[x][j] != '.':
if board[x][j] == 'p':
result += 1
break
for j in range(y-1,-1,-1):
if board[x][j] != '.':
if board[x][j] == 'p':
result += 1
break
return result