class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
}
};
999. 可以被一步捕获的棋子数
在一个 8 x 8 的棋盘上,有一个白色的车(Rook
),用字符 'R'
表示。棋盘上还可能存在空方块,白色的象(Bishop
)以及黑色的卒(pawn
),分别用字符 '.'
,'B'
和 'p'
表示。不难看出,大写字符表示的是白棋,小写字符表示的是黑棋。
车按国际象棋中的规则移动。东,西,南,北四个基本方向任选其一,然后一直向选定的方向移动,直到满足下列四个条件之一:
你现在可以控制车移动一次,请你统计有多少敌方的卒处于你的捕获范围内(即,可以被一步捕获的棋子数)。
示例 1:
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] 输出:3 解释: 在本例中,车能够捕获所有的卒。
示例 2:
输入:[[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] 输出:0 解释: 象阻止了车捕获任何卒。
示例 3:
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]] 输出:3 解释: 车可以捕获位置 b5,d6 和 f5 的卒。
提示:
board.length == board[i].length == 8
board[i][j]
可以是 'R'
,'.'
,'B'
或 'p'
board[i][j] == 'R'
原站题解
cpp 解法, 执行用时: 0 ms, 内存消耗: 8.9 MB, 提交时间: 2024-12-06 07:25:18
class Solution { static constexpr int DIRS[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; public: int numRookCaptures(vector<vector<char>>& board) { const int SIZE = 8; int x0, y0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == 'R') { x0 = i; y0 = j; } } } int ans = 0; for (auto& [dx, dy] : DIRS) { int x = x0 + dx, y = y0 + dy; while (0 <= x && x < SIZE && 0 <= y && y < SIZE && board[x][y] == '.') { x += dx; y += dy; } if (0 <= x && x < SIZE && 0 <= y && y < SIZE && board[x][y] == 'p') { ans++; } } return ans; } };
java 解法, 执行用时: 0 ms, 内存消耗: 40.1 MB, 提交时间: 2024-12-06 07:25:05
class Solution { private static final int[][] DIRS = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; public int numRookCaptures(char[][] board) { final int SIZE = 8; int x0 = 0; int y0 = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == 'R') { x0 = i; y0 = j; } } } int ans = 0; for (int[] d : DIRS) { int x = x0 + d[0]; int y = y0 + d[1]; while (0 <= x && x < SIZE && 0 <= y && y < SIZE && board[x][y] == '.') { x += d[0]; y += d[1]; } if (0 <= x && x < SIZE && 0 <= y && y < SIZE && board[x][y] == 'p') { ans++; } } return ans; } }
golang 解法, 执行用时: 0 ms, 内存消耗: 3.8 MB, 提交时间: 2024-12-06 07:24:48
var dirs = []struct{ x, y int }{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} func numRookCaptures(board [][]byte) (ans int) { const size = 8 var x0, y0 int for i, row := range board { for j, c := range row { if c == 'R' { x0, y0 = i, j } } } for _, d := range dirs { x, y := x0+d.x, y0+d.y for 0 <= x && x < size && 0 <= y && y < size && board[x][y] == '.' { x += d.x y += d.y } if 0 <= x && x < size && 0 <= y && y < size && board[x][y] == 'p' { ans++ } } return }
python3 解法, 执行用时: 0 ms, 内存消耗: 17 MB, 提交时间: 2024-12-06 07:24:16
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: SIZE = 8 for i, row in enumerate(board): for j, c in enumerate(row): if c == 'R': x0, y0 = i, j ans = 0 for dx, dy in (0, -1), (0, 1), (-1, 0), (1, 0): x, y = x0 + dx, y0 + dy while 0 <= x < SIZE and 0 <= y < SIZE and board[x][y] == '.': x += dx y += dy if 0 <= x < SIZE and 0 <= y < SIZE and board[x][y] == 'p': ans += 1 return ans
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2021-06-12 00:18:07
func numRookCaptures(board [][]byte) (cnt int) { ri, rj := 0, 0 for i := 0; i < 8; i++ { for j := 0; j < 8; j++ { if board[i][j] == 'R' { ri, rj = i, j break } } } tx, ty := 0, 0 // x轴,y轴 dx, dy := [4]int{0, 1, 0, -1}, [4]int{1, 0, -1, 0} for i := 0; i < 4; i++ { for step := 0; ; step++ { tx = ri + step * dx[i]; ty = rj + step * dy[i]; if tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B' { break } if board[tx][ty] == 'p' { cnt++ break } } } return cnt }