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
Available Captures for Rook INPUT p R B p R = White Rook p = Black Pawn B = White Bishop 8x8 chessboard matrix Find R, scan 4 directions Rook at position [2,3] ALGORITHM STEPS 1 Find Rook Position Locate R at [2,3] 2 Scan 4 Directions UP, DOWN, LEFT, RIGHT R UP DOWN LEFT RIGHT 3 Check Each Path Stop at p (count++) Stop at B (blocked) 4 Return Count Total pawns attackable FINAL RESULT R p p p B UP: p found - OK LEFT: p found - OK DOWN: p found - OK RIGHT: B blocks path Output: 3 3 pawns can be captured! Key Insight: Directional Simulation: From the rook's position, traverse each direction until hitting a piece or edge. If you hit a black pawn 'p', increment count. If you hit a bishop 'B' or edge, stop that direction. TutorialsPoint - Available Captures for Rook | Directional Simulation Approach
Asked in
Amazon 15 Google 12
25.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen