999. Available Captures for Rook #
Problem #
On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters ‘R’, ‘.’, ‘B’, and ‘p’ respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.
The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.
Return the number of pawns the rook can capture in one move.
Example 1:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
In this example the rook is able to capture all the pawns.
Example 2:
Input: [[".",".",".",".",".",".",".","."],[".","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:
Bishops are blocking the rook to capture any pawn.
Example 3:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
The rook can capture the pawns at positions b5, d6 and f5.
Note:
board.length == board[i].length == 8board[i][j]is either'R','.','B', or'p'- There is exactly one cell with
board[i][j] == 'R'
Problem Summary #
On an 8 x 8 chessboard, there is a white rook, represented by the character ‘R’. The board may also contain empty squares, white bishops, and black pawns, represented by the characters ‘.’, ‘B’, and ‘p’ respectively. It is easy to see that uppercase characters represent white pieces, and lowercase characters represent black pieces. The rook moves according to the rules of chess. It chooses one of the four basic directions: east, west, south, or north, and then keeps moving in the chosen direction until one of the following four conditions is met:
- The player chooses to stop voluntarily.
- The piece stops because it reaches the edge of the board.
- The piece moves to a square to capture an enemy (black) pawn on that square and stops on that square.
- The rook cannot enter/pass through a square already occupied by another friendly piece (a white bishop), and stops in front of the friendly piece.
You can now control the rook to move once. Count how many enemy pawns are within your capture range (that is, the number of pieces that can be captured in one move).
Solution Approach #
- Move the rook according to the rules of chess. The requirement is to output how many pawns are within the rook’s capture range after moving only once.
- Easy problem. According to the movement rules of a chess rook, simply enumerate the 4 directions respectively.
Code #
package leetcode
func numRookCaptures(board [][]byte) int {
num := 0
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[i]); j++ {
if board[i][j] == 'R' {
num += caputure(board, i-1, j, -1, 0) // Up
num += caputure(board, i+1, j, 1, 0) // Down
num += caputure(board, i, j-1, 0, -1) // Left
num += caputure(board, i, j+1, 0, 1) // Right
}
}
}
return num
}
func caputure(board [][]byte, x, y int, bx, by int) int {
for x >= 0 && x < len(board) && y >= 0 && y < len(board[x]) && board[x][y] != 'B' {
if board[x][y] == 'p' {
return 1
}
x += bx
y += by
}
return 0
}