mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-29 15:14:53 +08:00
添加 16 题
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user