Q:

在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。

车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。

返回车能够在一次移动中捕获到的卒的数量。
 

示例 1:

【Leetcode_总结】999. 车的可用捕获量 - python

输入:
[
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。

链接:https://leetcode-cn.com/problems/available-captures-for-rook/description/

思路:笨方法

代码:

class Solution(object):
    def numRookCaptures(self, board):
        """
        :type board: List[List[str]]
        :rtype: int
        """
        row = []
        col = []
        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] == 'R':
                    row = board[i]
                    col = [board[k][j] for k in range(8)]
                    return self.check(row) + self.check(col)
    
    def check(self,input):
        index = input.index("R")
        left = input[:index]
        right = input[index+1:]
        count = 0
        while left:
            tmp = left.pop()
            if tmp == "p":
                count +=1
                break
            if tmp == 'B':
                break
                
        while right:
            tmp = right.pop(0)
            if tmp == "p":
                count +=1
                break
            if tmp == 'B':
                break
        return count
            

 

相关文章:

  • 2021-07-23
  • 2021-07-03
  • 2021-12-16
  • 2022-12-23
  • 2022-01-04
  • 2021-10-28
  • 2021-12-14
  • 2021-10-20
猜你喜欢
  • 2021-10-07
  • 2021-04-25
  • 2021-08-21
  • 2021-11-20
  • 2022-01-11
  • 2022-03-05
  • 2022-01-07
相关资源
相似解决方案