Available Captures for Rook

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 represented by 'B', and some number of black pawns represented by 'p'. Empty squares are represented by '.'.

A rook can move any number of squares horizontally or vertically: up, down, left, or right. It stops when 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
. . . . . . . .
. . . p . . . .
. . . R . . . p
. . . . . . . .
. . . . . . . .
. . . p . . . .
. . . . . . . .
. . . . . . . .
Inputboard = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output3
In this example, the rook is attacking all the pawns.
Example 2
. . . . . . . .
. 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 . .
. . . . . . . .
. . . . . . . .
Inputboard = [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output0
The bishops are blocking the rook from attacking any of the pawns.

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'

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate