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'
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
x, y, n = 0, 0, 8
for i in range(n):
for j in range(n):
if board[i][j] == 'R':
x, y = i, j
break
ans = 0
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
i, j = x, y
while 0 <= i + a < n and 0 <= j + b < n and board[i + a][j + b] != 'B':
i, j = i + a, j + b
if board[i][j] == 'p':
ans += 1
break
return ans
class Solution {
public int numRookCaptures(char[][] board) {
int[] pos = find(board);
int ans = 0, n = 8;
int[][] dirs = new int[][]{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
for (int[] dir : dirs) {
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
x += a;
y += b;
if (board[x][y] == 'p') {
++ans;
break;
}
}
}
return ans;
}
private int[] find(char[][] board) {
int n = 8;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'R') {
return new int[]{i, j};
}
}
}
return null;
}
}
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
vector<int> pos = find(board);
int ans = 0, n = 8;
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
for (auto& dir : dirs)
{
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B')
{
x += a;
y += b;
if (board[x][y] == 'p')
{
++ans;
break;
}
}
}
return ans;
}
vector<int> find(vector<vector<char>>& board) {
int n = 8;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == 'R')
{
return {i, j};
}
}
}
return {};
}
};
func numRookCaptures(board [][]byte) int {
n := 8
find := func() []int {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if board[i][j] == 'R' {
return []int{i, j}
}
}
}
return []int{}
}
pos := find()
ans := 0
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
for _, dir := range dirs {
x, y, a, b := pos[0], pos[1], dir[0], dir[1]
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != 'B' {
x += a
y += b
if board[x][y] == 'p' {
ans++
break
}
}
}
return ans
}