??999. 车的可用捕获量(Python)

更多精彩内容,请关注【力扣简单题】。

题目

难度:★★★☆☆
类型:几何,二维数组

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

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

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

大佬博客

class Solution(object):
    def numRookCaptures(self, board):
        """
        :type board: List[List[str]]
        :rtype: int
        """
        R_index = 0
        count = 0

        for line in board:
            if 'R' in line:
                R_index = line.index('R')
                l = line[:R_index]
                l.reverse()
                r = line[R_index+1:]
                print l , r
                for i in l:
                    if i == 'B':
                        break
                    if i == 'p':
                        count += 1
                        break
                for j in r:
                    if j == 'B':
                        break
                    if j == 'p':
                        count += 1
                        break
        print "count" , count
        v = [i[R_index] for i in board]
        v_index = v.index('R')
        up = v [:v_index]
        up.reverse()
        down =v [v_index+1:]
        for i in up:
            if i == 'B':
                break
            if i == 'p':
                count += 1
                break
        for j in down:
            if j == 'B':
                break
            if j == 'p':
                count += 1
                break
        return count```

如有疑问或建议,欢迎评论区留言~

你可能感兴趣的:(??999. 车的可用捕获量(Python))