Available Captures For Rook Problem
Description
LeetCode Problem 999.
On an 8 x 8 chessboard, there is exactly one white rook 'R' and some number of white bishops 'B', black pawns 'p', and empty squares '.'.
When the rook moves, it chooses one of four cardinal directions (north, east, south, or west), then moves in that direction until it chooses to stop, reaches the edge of the board, captures a black pawn, or is blocked by a white bishop. A rook is considered attacking a pawn if the rook can capture the pawn on the rook's turn. The number of available captures for the white rook is the number of pawns that the rook is attacking.
Return the number of available captures for the white rook.
Example 1:
Input: board =
[[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: In this example, the rook is attacking all the pawns.
Example 2:
Input: 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",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
Output: 0
Explanation: The bishops are blocking the rook from attacking any of the pawns.
Example 3:
Input: board =
[[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","p",".",".",".","."],
["p","p",".","R",".","p","B","."],
[".",".",".",".",".",".",".","."],
[".",".",".","B",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: The rook is attacking the pawns at positions b5, d6, and f5.
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'
Solution
Approach: Simulate the Four Directions
The board is only 8 × 8, so we simply look where the rook can go.
- Scan the board once to find the rook's row and column.
- For each of the four directions (north, south, west, east), step one square at a time from the rook:
- an empty square
'.'lets the rook keep sliding; - a pawn
'p'is a capture: count it and stop; - a bishop
'B'blocks the rook: stop without counting; - leaving the board also stops the search.
- an empty square
- Return the number of captures, which is at most 4.
The detail to get right: the rook stops at the first piece it meets in each direction. A pawn behind a bishop, or behind another pawn, cannot be captured, so the loop must stop at any non-empty square, not only at bishops. Using one list of direction offsets keeps the four walks identical and avoids four copied loops.
Complexity: O(1) time and O(1) space, since the board is always 8 × 8 (in general O(n2) to find the rook on an n × n board).
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
size = 8
# Find the rook.
rook_row, rook_col = 0, 0
for r in range(size):
for c in range(size):
if board[r][c] == "R":
rook_row, rook_col = r, c
captures = 0
for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)): # north, south, west, east
r, c = rook_row + dr, rook_col + dc
# Slide until the edge or the first piece in this direction.
while 0 <= r < size and 0 <= c < size:
if board[r][c] == "p":
captures += 1
if board[r][c] != ".":
break # a pawn is captured, a bishop blocks: either way the rook stops
r += dr
c += dc
return captures
Now write it from memory
Close the write-up and solve it yourself against the tests.