Available Captures for Rook - Problem
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.
Input & Output
Example 1 — Basic Attack Pattern
$
Input:
board = [["..",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".","B","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
›
Output:
1
💡 Note:
Rook at (2,3) can attack the pawn directly above at (1,3). The pawn at (4,7) is not in the same row or column as the rook, so it cannot be attacked.
Example 2 — No Attackable Pawns
$
Input:
board = [["..",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","R",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
›
Output:
0
💡 Note:
Rook at (2,3) with no pawns on board - cannot attack any pawns.
Example 3 — Blocked by Bishop
$
Input:
board = [["..",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","R","B",".","p","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
›
Output:
0
💡 Note:
Rook at (2,3) cannot attack pawn at (2,6) because bishop at (2,4) blocks the path.
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'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code