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:
3
💡 Note:
Rook at (2,3) can attack: pawn up at (1,3), and in right direction it's blocked by bishop so can't attack the pawn at (4,7). Wait, let me recalculate - rook can attack 1 pawn directly above it.
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
Understanding the Visualization
1
Input
8×8 chessboard with rook 'R', bishops 'B', and pawns 'p'
2
Process
Simulate rook movement in 4 directions until blocked
3
Output
Count of pawns that can be captured
Key Takeaway
🎯 Key Insight: Rook attacks in straight lines - simulate movement in 4 directions until blocked
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code