Available Captures for Rook
Problem
On an 8×8 chessboard, there is exactly one white rook 'R', some white bishops 'B', some black pawns 'p', and empty squares '.'. The rook moves horizontally or vertically until it chooses to stop, reaches the edge, captures a black pawn, or is blocked by a white bishop. Return the number of pawns the rook can capture in one move.
board with R at (4,3), pawns left/right/up, bishop blocking down3def num_rook_captures(board):
for r in range(8):
for c in range(8):
if board[r][c] == 'R':
rr, rc = r, c
captures = 0
for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
nr, nc = rr + dr, rc + dc
while 0 <= nr < 8 and 0 <= nc < 8:
if board[nr][nc] == 'B':
break
if board[nr][nc] == 'p':
captures += 1
break
nr += dr
nc += dc
return captures
function numRookCaptures(board) {
let rr, rc;
for (let r = 0; r < 8; r++)
for (let c = 0; c < 8; c++)
if (board[r][c] === 'R') { rr = r; rc = c; }
let captures = 0;
const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];
for (const [dr, dc] of dirs) {
let nr = rr + dr, nc = rc + dc;
while (nr >= 0 && nr < 8 && nc >= 0 && nc < 8) {
if (board[nr][nc] === 'B') break;
if (board[nr][nc] === 'p') { captures++; break; }
nr += dr; nc += dc;
}
}
return captures;
}
class Solution {
public int numRookCaptures(char[][] board) {
int rr = 0, rc = 0;
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
if (board[r][c] == 'R') { rr = r; rc = c; }
int captures = 0;
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] d : dirs) {
int nr = rr + d[0], nc = rc + d[1];
while (nr >= 0 && nr < 8 && nc >= 0 && nc < 8) {
if (board[nr][nc] == 'B') break;
if (board[nr][nc] == 'p') { captures++; break; }
nr += d[0]; nc += d[1];
}
}
return captures;
}
}
int numRookCaptures(vector<vector<char>>& board) {
int rr = 0, rc = 0;
for (int r = 0; r < 8; r++)
for (int c = 0; c < 8; c++)
if (board[r][c] == 'R') { rr = r; rc = c; }
int captures = 0;
int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (auto& d : dirs) {
int nr = rr + d[0], nc = rc + d[1];
while (nr >= 0 && nr < 8 && nc >= 0 && nc < 8) {
if (board[nr][nc] == 'B') break;
if (board[nr][nc] == 'p') { captures++; break; }
nr += d[0]; nc += d[1];
}
}
return captures;
}
Explanation
A rook attacks in four straight lines — up, down, left, right — until something stops it. So the plan is: find the rook, then fire a ray in each of the four directions and see what it hits first.
We first scan the 8x8 board to locate the rook's coordinates (rr, rc). Then we loop over the four direction vectors (-1,0), (1,0), (0,-1), (0,1), each one nudging row/column by a fixed step.
Walking outward in a direction, the first piece we meet decides everything: a bishop 'B' blocks the path, so we break with no capture; a pawn 'p' means a capture, so we add 1 and break. Empty squares '.' let us keep moving until we fall off the edge.
Example: a rook with pawns reachable to its left, right, and up, but a bishop sitting between it and the pawn below. Three rays reach a pawn first and one is blocked, so the count is 3.