Leetcode 1041 Solution

This article provides solution to leetcode question 1041 (available-captures-for-rook)

https://leetcode.com/problems/available-captures-for-rook

Solution

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        for ri, rj in itertools.product(range(8), range(8)):
            if board[ri][rj] == "R":
                break

        ans = 0

        i, j = ri, rj
        while i >= 0:
            if board[i][j] in [".", "R"]:
                i -= 1
                continue
            if board[i][j] == "p":
                ans += 1
            break

        i, j = ri, rj
        while i < 8:
            if board[i][j] in [".", "R"]:
                i += 1
                continue
            if board[i][j] == "p":
                ans += 1
            break

        i, j = ri, rj
        while j >= 0:
            if board[i][j] in [".", "R"]:
                j -= 1
                continue
            if board[i][j] == "p":
                ans += 1
            break

        i, j = ri, rj
        while j < 8:
            if board[i][j] in [".", "R"]:
                j += 1
                continue
            if board[i][j] == "p":
                ans += 1
            break

        return ans