Available Captures for Rook

More downvotes. Problem statement is confusing.

We can solve this problem by first finding the position of the rook on the board, and then checking each of the four cardinal directions for pawns (‘p’) that can be captured. The directions are north (up), south (down), east (right), and west (left).

Here is a Python solution using this approach:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        # Find the rook's position
        for i in range(8):
            for j in range(8):
                if board[i][j] == 'R':
                    x, y = i, j

        # Directions for north, south, east, west
        directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        captures = 0

        # Check each direction for a pawn capture
        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            while 0 <= nx < 8 and 0 <= ny < 8:
                if board[nx][ny] == 'B':
                    break
                if board[nx][ny] == 'p':
                    captures += 1
                    break
                nx, ny = nx + dx, ny + dy

        return captures

This code first loops over the entire board to find the rook. Once the rook is found, it checks each of the four cardinal directions until it encounters a bishop (‘B’), the edge of the board, or a pawn (‘p’). If it encounters a pawn, it increments the captures counter and breaks out of the current direction. The number of captures is then returned.