Skip to main content

Available Captures for Rook - Solution & Explanation

EasyArrayMatrixSimulation23 min readAsked at: Google, Square
Practice this problem

Problem Statement

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 == 8
  • board[i].length == 8
  • board[i][j] is either 'R', '.', 'B', or 'p'
  • There is exactly one cell with board[i][j] == 'R'

Approach Overview

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 1: Direction-Based Iteration

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Multi-Directional Traversal with Early Exit

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 3: Simulation

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.

  • If it is not the boundary and not a bishop, continue moving forward.
  • If it is a pawn, increment the answer by one and stop traversing in that direction.

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).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(1), as no additional storage is required.

Multi-Directional Traversal with Early Exit

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.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direction-Based IterationO(n)O(1)Standard solution for rook movement simulation on a grid
Multi-Directional Traversal with Early ExitO(n)O(1)Cleaner directional logic using vectors and early stopping

Video Solution

LeetCode Algorithms Easy: Available Captures for Rook • Mike the Coder • 1,222 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Available Captures for Rook easy or hard?
Available Captures for Rook is classified as an Easy problem. The challenge mainly involves correctly simulating rook movement and stopping traversal when a blocking piece appears. Basic matrix traversal knowledge is enough to solve it.
Available Captures for Rook Python/Java solution
Python and Java implementations both follow the same pattern: find the rook position, then iterate in four directions until encountering a pawn or bishop. The logic uses simple loops and condition checks, maintaining O(n) time and O(1) extra space.
How to solve Available Captures for Rook in O(n)?
First scan the matrix to find the rook's coordinates. From that position, iterate outward in four directions using loops or directional vectors. Stop traversal when a bishop appears or count a pawn when encountered. Each direction is explored once, giving an overall O(n) runtime.
What is the best approach for Available Captures for Rook?
Direction-based iteration is the most common solution. After locating the rook, scan in four directions (up, down, left, right) until a bishop blocks the path or a pawn is captured. This simulation approach runs in O(n) time and O(1) space and directly models the rook's chess movement.
Is Available Captures for Rook asked at Google/Amazon/Meta?
Grid traversal and simulation problems like Available Captures for Rook appear in interviews at companies such as Amazon and Google. They test understanding of matrix iteration, boundary checks, and directional traversal patterns.
What data structure is used in Available Captures for Rook?
The problem primarily uses a 2D array (matrix) to represent the chessboard. The algorithm performs directional traversal across rows and columns, making it a classic matrix simulation problem.
What is the time complexity of Available Captures for Rook?
The time complexity is O(n) where n is the board dimension. You scan the board once to locate the rook and then traverse at most 8 cells in each of four directions. Space complexity is O(1) since the algorithm uses only a few variables.

Ready to solve this problem?

Practice Available Captures for Rook with our built-in code editor and test cases.

Practice on FleetCode