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.

Examples

Example 1

1
2
3
4
5
Input: board =
[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
In this example, the rook is attacking all the pawns.

Example 2

1
2
3
4
5
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

1
2
3
4
5
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

Method 1 – Directional Scan

Intuition

The rook can attack pawns in its row and column until it hits a blocking piece (bishop or board edge). By scanning in all four directions from the rook’s position, we can count all pawns it can attack.

Approach

  1. Find the position (r, c) of the rook 'R' on the board.
  2. For each of the four directions (up, down, left, right):
  • Move step by step from the rook’s position.
  • If a bishop 'B' is encountered, stop scanning in that direction.
  • If a pawn 'p' is encountered, increment the answer and stop scanning in that direction.
  • If an empty square '.' is encountered, continue scanning.
  1. Return the total count of pawns the rook can attack.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
   int numRookCaptures(vector<vector<char>>& board) {
      int ans = 0, r = 0, c = 0;
      for (int i = 0; i < 8; ++i)
        for (int j = 0; j < 8; ++j)
           if (board[i][j] == 'R') { r = i; c = j; }
      int dirs[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
      for (auto& d : dirs) {
        int x = r + d[0], y = c + d[1];
        while (x >= 0 && x < 8 && y >= 0 && y < 8) {
           if (board[x][y] == 'B') break;
           if (board[x][y] == 'p') { ++ans; break; }
           x += d[0]; y += d[1];
        }
      }
      return ans;
   }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
   public int numRookCaptures(char[][] board) {
      int ans = 0, r = 0, c = 0;
      for (int i = 0; i < 8; ++i)
        for (int j = 0; j < 8; ++j)
           if (board[i][j] == 'R') { r = i; c = j; }
      int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};
      for (int[] d : dirs) {
        int x = r + d[0], y = c + d[1];
        while (x >= 0 && x < 8 && y >= 0 && y < 8) {
           if (board[x][y] == 'B') break;
           if (board[x][y] == 'p') { ++ans; break; }
           x += d[0]; y += d[1];
        }
      }
      return ans;
   }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
   def numRookCaptures(self, board: list[list[str]]) -> int:
      ans = 0
      r = c = 0
      for i in range(8):
        for j in range(8):
           if board[i][j] == 'R':
              r, c = i, j
      for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
        x, y = r + dr, c + dc
        while 0 <= x < 8 and 0 <= y < 8:
           if board[x][y] == 'B':
              break
           if board[x][y] == 'p':
              ans += 1
              break
           x += dr
           y += dc
      return ans

Complexity

  • ⏰ Time complexity: O(1) (since the board is always 8x8, all operations are constant time)
  • 🧺 Space complexity: O(1) (no extra space used except a few variables)