## 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...= 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 & Input x – + cmd board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] Output x – + cmd 3 ```
``` #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 & Input x – + cmd board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] Output x – + cmd 3 #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 & Input x – + cmd 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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] #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 & Input x – + cmd 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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] Advertisements (adsbygoogle = window.adsbygoogle || []).push({}); Demonstration ```
``` #998 Leetcode Maximum Binary Tree II Solution in C, C++, Java, JavaScript, Python, C# Leetcode #1000 Leetcode Minimum Cost to Merge Stones Solution in C, C++, Java, JavaScript, Python, C# Leetcode ```
``` ```