You are given an 8 x 8 matrix representing a chessboard. There is exactly one white rook represented by '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.
Example 1:
Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
In this example, the rook is attacking all the pawns.
Example 2:
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:
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 == 8board[i].length == 8board[i][j] is either 'R', '.', 'B', or 'p'board[i][j] == 'R'Problem Overview: You’re given an 8x8 chessboard represented as a matrix. A white rook 'R' can move horizontally or vertically until it hits another piece. Pawns 'p' can be captured, while bishops 'B' block the rook’s path. The task is to count how many pawns the rook can capture in the four cardinal directions.
Approach 1: Direction-Based Iteration (O(n) time, O(1) space)
First locate the rook’s position by scanning the board. Once found, simulate the rook’s movement in the four directions: up, down, left, and right. For each direction, iterate step-by-step through the board. If you encounter a bishop 'B', the path is blocked and you stop exploring that direction. If you encounter a pawn 'p', increment the capture count and stop because the rook captures only the first piece in that direction.
This approach relies on simple iteration over a matrix and direct simulation of chess movement rules. The key insight is that the rook only cares about the first non-empty square in each direction. Because there are only four directions and at most 8 cells per direction, the traversal is extremely efficient. Time complexity is O(n) where n represents the board dimension, and space complexity remains O(1) since no extra structures are needed.
Approach 2: Multi-Directional Traversal with Early Exit (O(n) time, O(1) space)
This variation treats each direction as an independent traversal starting from the rook. Instead of scanning the board multiple times, you locate the rook once and then expand outward in four directions using directional vectors like (-1,0), (1,0), (0,-1), and (0,1). Each step checks the current square and exits immediately when a blocking bishop or capturable pawn appears.
The early-exit behavior reduces unnecessary checks and keeps the logic clean. The implementation closely resembles typical array or simulation patterns used in grid problems. Every direction stops as soon as a decision is made, ensuring no redundant traversal. The complexity remains O(n) time and O(1) space, but the structure is easier to generalize to larger boards or similar movement problems.
Recommended for interviews: Direction-based iteration is what interviewers expect. It directly models rook movement and shows you understand grid traversal and early stopping conditions. A brute-force board scan shows basic understanding, but directional traversal demonstrates stronger control over matrix simulation patterns.
This approach involves finding the rook on the board first. Once located, traverse from the rook's position in all four possible directions (up, down, left, right) until you either reach the edge of the board or hit a piece (bishop or pawn). If you encounter a pawn ('p') before a blocking piece or the edge, count it as an attack.
This C implementation locates the rook first and then checks each direction—down, up, right, and left—for potential captures of pawns. Each direction continues until a capturing opportunity is found, a bishop blocks the path, or the board's boundary is reached.
Time Complexity: O(N) where N is the number of squares potentially traversed; in the worst case, around 28 to 32 cells in total.
Space Complexity: O(1), as no additional storage is required.
This approach also begins by locating the rook but applies a multi-threaded like traversal where each direction is explored and can theoretically break free once the path is obstructed by a Bishop or edge effectively.
This solution traverses the board from the rook's position in all four directions, measuring in single-unit steps to potentially reach pawn captures with minimal conditional checks beyond boundary failures and encountering bishops.
Time Complexity: O(1), given that the fixed 8x8 space constrains potential traversals inherently limiting step analysis.
Space Complexity: O(1), with the directional vectors being the primary variable dependency.
We first traverse the board to find the position of the rook (i, j). Then, starting from (i, j), we traverse in four directions: up, down, left, and right.
After traversing in all four directions, we get the answer.
The time complexity is O(m times n), where m and n are the number of rows and columns of the board, respectively. In this problem, m = n = 8. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Direction-Based Iteration | Time Complexity: O(N) where N is the number of squares potentially traversed; in the worst case, around 28 to 32 cells in total. |
| Multi-Directional Traversal with Early Exit | Time Complexity: O(1), given that the fixed 8x8 space constrains potential traversals inherently limiting step analysis. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direction-Based Iteration | O(n) | O(1) | Standard solution for rook movement simulation on a grid |
| Multi-Directional Traversal with Early Exit | O(n) | O(1) | Cleaner directional logic using vectors and early stopping |
LeetCode#999 Available Captures for Rook - Python • CodeJulian • 1,334 views views
Watch 9 more video solutions →Practice Available Captures for Rook with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor