Difficulty: Easy | Acceptance: 71.70% | Paid: No Topics: Array, Matrix, Simulation
On an 8 x 8 chessboard, there is exactly one white rook ‘R’ and some number of white bishops ‘B’, black pawns ‘p’, and empty squares ’.‘.
When the rook moves, it can choose to move to any square in the same row or column, or it can choose to capture a pawn. The rook cannot move through any piece (bishop or pawn).
Return the number of pawns the rook can capture in one move.
- Examples
- Constraints
- Direction Array Search
- Explicit Direction Checks
Examples
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'
Direction Array Search
Intuition Find the rook’s position, then use direction vectors to explore all four cardinal directions until hitting a piece or board edge.
Steps
- Locate the rook by scanning the 8x8 board
- Define four direction vectors: up, down, left, right
- For each direction, move step by step until finding a pawn (count it), bishop (stop), or edge (stop)
- Return total captures
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
rook_row, rook_col = 0, 0
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
rook_row, rook_col = i, j
break
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
captures = 0
for dr, dc in directions:
r, c = rook_row + dr, rook_col + dc
while 0 <= r < 8 and 0 <= c < 8:
if board[r][c] == 'p':
captures += 1
break
if board[r][c] == 'B':
break
r += dr
c += dc
return captures
Complexity
- Time: O(1) - Fixed 8x8 board, at most 64 cells to scan plus 4 directions with max 7 steps each
- Space: O(1) - Only using a few variables regardless of input size
- Notes: Direction array approach is clean and easily extensible for similar grid problems
Explicit Direction Checks
Intuition Same logic as direction array, but explicitly write out each direction check for clarity and to avoid loop overhead.
Steps
- Find the rook’s position by scanning the board
- Check upward direction: iterate from rook-1 to 0, count pawn if found, stop at bishop
- Check downward direction: iterate from rook+1 to 7, count pawn if found, stop at bishop
- Check left direction: iterate from rook-1 to 0, count pawn if found, stop at bishop
- Check right direction: iterate from rook+1 to 7, count pawn if found, stop at bishop
- Return total count
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
rook_row, rook_col = 0, 0
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
rook_row, rook_col = i, j
break
captures = 0
for i in range(rook_row - 1, -1, -1):
if board[i][rook_col] == 'p':
captures += 1
break
if board[i][rook_col] == 'B':
break
for i in range(rook_row + 1, 8):
if board[i][rook_col] == 'p':
captures += 1
break
if board[i][rook_col] == 'B':
break
for j in range(rook_col - 1, -1, -1):
if board[rook_row][j] == 'p':
captures += 1
break
if board[rook_row][j] == 'B':
break
for j in range(rook_col + 1, 8):
if board[rook_row][j] == 'p':
captures += 1
break
if board[rook_row][j] == 'B':
break
return captures
Complexity
- Time: O(1) - Fixed board size means constant operations
- Space: O(1) - Only uses a fixed number of variables
- Notes: More verbose but eliminates direction array overhead; both approaches are equally efficient for this problem size