Watch 10 video solutions for Available Captures for Rook, a easy level problem involving Array, Matrix, Simulation. This walkthrough by CodeJulian has 1,334 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |