Problem Description
Given an 8x8 chessboard represented as a matrix, determine the number of black pawns ('p') that a white rook ('R') can capture in one move. The rook can move horizontally or vertically in straight lines until it is blocked by another piece (bishop 'B' or any pawn) or the edge of the board.
Key Insights
- The board is fixed in size (8x8), so a complete scan is efficient.
- The first step is to locate the position of the white rook.
- From the rook's position, check in the four possible directions (up, down, left, right).
- Stop searching in a direction as soon as a bishop is encountered or when encountering a pawn (increment count if pawn found).
- Since pieces block the rook’s path, no further moves can be taken past the first encountered piece in any direction.
Space and Time Complexity
Time Complexity: O(1) (The board is always 8x8, so all operations are constant time.) Space Complexity: O(1) (No extra space beyond a few variables is used.)
Solution
We first locate the rook by scanning the board. Then for each of the four directions (up, down, left, right), we iterate from the rook's position until either the edge of the board or a blocking piece is reached. If a pawn is encountered before a blocking bishop, it is captured, and we increment the count. This approach uses a simple simulation technique and iterates over fixed dimensions, making it efficient and easy to understand.