File: available-captures-for-rook/solution.py

Date: 2026-06-06

Time: 15:17

Available Captures for Rook — solution.py

Purpose

Solves LeetCode 999: Available Captures for Rook. Given an 8x8 chessboard, find the white rook ('R') and count how many black pawns ('p') it can capture by moving in the four cardinal directions, where white bishops ('B') block its path.

Key Components

Solution.regionsBySlashes — The method name is wrong; it belongs to LeetCode 999 (rook captures), not problem 959 (regions by slashes). The docstring is correct, the name isn't. This likely resulted from a code-generation or copy-paste error. The LeetCode judge doesn't care about method names in Python submissions, but it will confuse anyone reading the code.

Parameters: board: List[List[str]] — an 8x8 grid where cells are one of 'R', 'B', 'p', or '.'.

Returns: int — number of capturable pawns (0 to 4).

Flow

1. Locate the rook (lines 12-15): Brute-force scan of all 64 cells. Stores position in (rr, rc). If no rook exists, rr and rc default to (0, 0) — no explicit guard.

2. Probe four directions (lines 17-24): For each cardinal direction (dr, dc), walk outward from the rook one cell at a time:

Patterns

Dependencies

Invariants

Error Handling

None. The code trusts the input matches the problem constraints. No validation of board dimensions, piece characters, or rook existence.

Topics to Explore

Beliefs