Algorithm
Problem Name: 999. Available Captures for Rook
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'
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
start coding...
class Solution {
int[][] dirs = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
public int numRookCaptures(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 'R') {
return getPawnCount(board, i, j);
}
}
}
return 0;
}
private int getPawnCount(char[][] board, int i, int j) {
int count = 0;
for (int[] dir : dirs) {
int newX = i + dir[0];
int newY = j + dir[1];
while (newX >= 0 && newX < board.length && newY >= 0 && newY < board[0].length) {
if (board[newX][newY] == 'p') {
count++;
break;
}
else if (board[newX][newY] == 'B') {
break;
}
newX = newX + dir[0];
newY = newY + dir[1];
}
}
return count;
}
}
code>
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const numRookCaptures = function(board) {
for (let i = 0; i < board.length; ++i)
for (let j = 0; j < board[i].length; ++j)
if (board[i][j] == 'R') return cap(board,i,j,0,1)+cap(board,i,j,0,-1)+cap(board,i,j,1,0)+cap(board,i,j,-1,0);
return 0;
};
function cap(b, x, y, dx, dy) {
while (x >= 0 && x < b.length && y >= 0 && y < b[x].length && b[x][y] != 'B') {
if (b[x][y] == 'p') return 1;
x += dx; y += dy;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def numRookCaptures(self, board: List[List[str]], res = 0) -> int:
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
for x in range(i - 1, -1, -1):
if board[x][j] in 'Bp':
res += board[x][j] == 'p'
break
for x in range(i + 1, 8):
if board[x][j] in 'Bp':
res += board[x][j] == 'p'
break
for y in range(j - 1, -1, -1):
if board[i][y] in 'Bp':
res += board[i][y] == 'p'
break
for y in range(j + 1, 8):
if board[i][y] in 'Bp':
res += board[i][y] == 'p'
break
return res
Copy The Code &
Try With Live Editor
Input
#4 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _0999_AvailableCapturesForRook
{
public int NumRookCaptures(char[][] board)
{
(var row, var col) = FindRook(board);
int count = 0;
for (var i = row - 1; i >= 0; i--)
{
if (board[i][col] == 'p')
{
count++;
break;
}
else if (board[i][col] == 'B')
break;
}
for (var i = row + 1; i < 8; i++)
{
if (board[i][col] == 'p')
{
count++;
break;
}
else if (board[i][col] == 'B')
break;
}
for (var i = col - 1; i >= 0; i--)
{
if (board[row][i] == 'p')
{
count++;
break;
}
else if (board[row][i] == 'B')
break;
}
for (var i = col + 1; i < 8; i++)
{
if (board[row][i] == 'p')
{
count++;
break;
}
else if (board[row][i] == 'B')
break;
}
return count;
}
private (int row, int col) FindRook(char[][] board)
{
for (var i = 0; i < 8; i++)
for (var j = 0; j < 8; j++)
if (board[i][j] == 'R')
return (i, j);
return (-1, -1);
}
}
}
Copy The Code &
Try With Live Editor
Input