> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Available Captures for Rook Python Solution

> Tested Python solution for LeetCode 999 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 999, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/available-captures-for-rook/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 999   # by problem number
lcpy gen -s available_captures_for_rook   # by problem name
```

## Problem

You are given an `8 x 8` matrix representing a chessboard. There is exactly one white rook represented as `'R'`, some number of white bishops `'B'`, and some number of black pawns `'p'`. Empty squares are represented by `'.'`.

A rook can move any number of squares horizontally or vertically (up, down, left, right) until it reaches another piece or the edge of the board. A rook is attacking a pawn if it can move to the pawn's square in one move.

Note: A rook cannot move through other pieces, such as bishops or pawns. This means a rook cannot attack a pawn if there is another piece blocking the path.

Return the number of pawns the white rook is attacking.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG)

```
Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: In this example, the rook is attacking all the pawns.
```

![Example 2](https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG)

```
Input: board = [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0
Explanation: The bishops are blocking the rook from attacking any of the pawns.
```

![Example 3](https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG)

```
Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: The rook is attacking the pawns at positions b5, d6, and f5.
```

### Constraints

* `board.length == 8`
* `board[i].length == 8`
* `board[i][j]` is either `'R'`, `'.'`, `'B'`, or `'p'`
* There is exactly one cell with `board[i][j] == 'R'`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/available_captures_for_rook/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/available_captures_for_rook/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1) — bounded 8x8 scan in four directions
    # Space: O(1)
    def num_rook_captures(self, board: list[list[str]]) -> int:
        rook_i = rook_j = -1
        for i in range(8):
            for j in range(8):
                if board[i][j] == "R":
                    rook_i, rook_j = i, j
        count = 0
        for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            i, j = rook_i + di, rook_j + dj
            while 0 <= i < 8 and 0 <= j < 8 and board[i][j] == ".":
                i, j = i + di, j + dj
            if 0 <= i < 8 and 0 <= j < 8 and board[i][j] == "p":
                count += 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(1) — bounded 8x8 scan in four directions | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.