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.
For example:
board = [
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]
]
In this example, the rook is attacking all the pawns, so the correct output is 3.
As another example:
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",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]
]
In this case, the correct output is 0 because the bishops are blocking the rook from attacking any of the pawns.
Write a function to solve this problem.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine the rook is on a chessboard, and we want to see how many pawns it can capture. We'll check every possible direction the rook can move and stop when it hits a friendly piece, an enemy pawn (which it can capture), or the edge of the board.
Here's how the algorithm would work step-by-step:
def available_captures(board): rook_row, rook_col = -1, -1
for row in range(8):
for col in range(8):
if board[row][col] == 'R':
rook_row, rook_col = row, col
break
if rook_row != -1:
break
captures = 0
# Check right
for col in range(rook_col + 1, 8):
if board[rook_row][col] == 'p':
captures += 1
# Stop looking once a pawn is found
break
elif board[rook_row][col] != '.':
# Stop if another piece is encountered
break
# Check left
for col in range(rook_col - 1, -1, -1):
if board[rook_row][col] == 'p':
captures += 1
# Stop looking once a pawn is found
break
elif board[rook_row][col] != '.':
# Stop if another piece is encountered
break
# Check up
for row in range(rook_row - 1, -1, -1):
if board[row][rook_col] == 'p':
captures += 1
# Stop looking once a pawn is found
break
elif board[row][rook_col] != '.':
# Stop if another piece is encountered
break
# Check down
for row in range(rook_row + 1, 8):
if board[row][rook_col] == 'p':
captures += 1
# Stop looking once a pawn is found
break
elif board[row][rook_col] != '.':
# Stop if another piece is encountered
break
return captures
The problem asks to find how many pawns a rook can capture on a chessboard. The optimal solution involves checking in all four directions from the rook until you hit an obstacle (another piece) or the edge of the board.
Here's how the algorithm would work step-by-step:
def numRookCaptures(board): rook_row = -1
rook_col = -1
for row in range(8):
for col in range(8):
if board[row][col] == 'R':
rook_row = row
rook_col = col
break
if rook_row != -1:
break
captures = 0
# Check left
for col in range(rook_col - 1, -1, -1):
if board[rook_row][col] == 'p':
# Stop after first pawn
captures += 1
break
elif board[rook_row][col] != '.':
# Stop after other piece
break
# Check right
for col in range(rook_col + 1, 8):
if board[rook_row][col] == 'p':
# Stop after first pawn
captures += 1
break
elif board[rook_row][col] != '.':
# Stop after other piece
break
# Check up
for row in range(rook_row - 1, -1, -1):
if board[row][rook_col] == 'p':
# Stop after first pawn
captures += 1
break
elif board[row][rook_col] != '.':
# Stop after other piece
break
# Check down
for row in range(rook_row + 1, 8):
if board[row][rook_col] == 'p':
# Stop after first pawn
captures += 1
break
elif board[row][rook_col] != '.':
# Stop after other piece
break
return captures
Case | How to Handle |
---|---|
Null or invalid board input | Throw an IllegalArgumentException or return 0 if the board is null or not an 8x8 array, to ensure program stability. |
Rook is placed on the edge of the board | The solution must handle cases where the rook's movement is constrained by the board edges without causing out-of-bounds errors. |
The board is completely empty (no pieces other than the rook) | The solution should return 0 because the rook cannot capture anything if there are no pawns. |
The board is completely full of enemy pawns with no friendly pieces between rook and pawns | The solution should count all pawns visible from the rook as capturable, not exceeding the maximum count of 4. |
Multiple pawns and blocking pieces exist in the same row/column | The algorithm should only count the first capturable pawn in each direction, stopping when it encounters a friendly piece or the board edge. |
Rook is surrounded by friendly pieces preventing any captures | The solution should return 0 as no captures are possible when the rook is blocked in all directions. |
Board contains invalid characters besides 'R', 'p', 'B', and '.' | The code should either ignore invalid characters or throw an exception to indicate malformed board input. |
Integer overflow on extremely large board sizes (though the problem constrains to 8x8) | Since board size is fixed, integer overflow is not possible; if input size scaled, integer types must be validated or use bigger types. |