Domain 1EasyCORE

Available Captures for Rook

The problem involves determining the number of black pawns that a white rook can capture on an 8x8 chessboard. The rook can move in four cardinal directions (north, east, south, west) until it either captures a pawn, is blocked by a bishop, or reaches the edge of the board. The input is a 2D array representing the chessboard, and the output is an integer representing the number of pawns the rook can capture.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

999. Available Captures for Rook

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 0
Acceptance rate: 67.8%


Problem Breakdown

The problem involves determining the number of black pawns that a white rook can capture on an 8x8 chessboard. The rook can move in four cardinal directions (north, east, south, west) until it either captures a pawn, is blocked by a bishop, or reaches the edge of the board. The input is a 2D array representing the chessboard, and the output is an integer representing the number of pawns the rook can capture.

Inputs:

  • A 2D list board of size 8x8 where each element is either 'R' (rook), 'B' (bishop), 'p' (pawn), or '.' (empty square).

Outputs:

  • An integer representing the number of pawns the rook can capture.

Implicit Constraints:

  • There is exactly one rook on the board.
  • The board dimensions are fixed at 8x8.

Intuition

A brute-force approach would involve checking all possible moves from the rook’s position in each direction until a pawn, bishop, or edge of the board is encountered. This approach is feasible given the small size of the board (8x8) and would have a time complexity of O(1) since the number of operations is bounded by the board size.

The key insight is to efficiently traverse the board in each direction from the rook’s position until a pawn, bishop, or edge is encountered. This ensures that we only check relevant positions without unnecessary iterations.

Approach

  1. Locate the Rook: Traverse the board to find the position of the rook 'R'.
  2. Check Each Direction: From the rook’s position, traverse in each of the four cardinal directions (north, east, south, west) until a pawn 'p', bishop 'B', or edge of the board is encountered.
  3. Count Captures: For each direction, if a pawn is encountered before any other piece, increment the capture count.

Complexity Analysis

  • Time Complexity: O(1), since the number of operations is bounded by the fixed size of the board (8x8) and the constant number of directions (4).
  • Space Complexity: O(1), as no additional space proportional to the input size is used.

Solution

def numRookCaptures(board):
    # Locate the rook's position
    for i in range(8):
        for j in range(8):
            if board[i][j] == 'R':
                rook_row, rook_col = i, j
                break
    
    # Directions: north, east, south, west
    directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    captures = 0
    
    for dr, dc in directions:
        r, c = rook_row + dr, rook_col + dc
        while 0 <= r < 8 and 0 <= c < 8:
            if board[r][c] == 'p':
                captures += 1
                break
            elif board[r][c] != '.':
                break
            r += dr
            c += dc
    
    return captures

Edge Cases Handled

  • No Pawns: If there are no pawns on the board, the function returns 0.
  • Bishops Blocking All Directions: If bishops block all directions from the rook, the function returns 0.
  • Pawns in Multiple Directions: The function correctly counts pawns that can be captured in multiple directions.