1 Star 0 Fork 332

卜月航 / leetcode

forked from doocs / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 8.83 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2022-09-06 15:05 . docs: code format

999. 可以被一步捕获的棋子数

English Version

题目描述

在一个 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 的卒。

 

提示:

  1. board.length == board[i].length == 8
  2. board[i][j] 可以是 'R''.''B' 或 'p'
  3. 只有一个格子上存在 board[i][j] == 'R'

解法

先找到 R 的位置,之后向“上、下、左、右”四个方向查找,累加结果。

Python3

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        x, y, n = 0, 0, 8
        for i in range(n):
            for j in range(n):
                if board[i][j] == 'R':
                    x, y = i, j
                    break
        ans = 0
        for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
            i, j = x, y
            while 0 <= i + a < n and 0 <= j + b < n and board[i + a][j + b] != 'B':
                i, j = i + a, j + b
                if board[i][j] == 'p':
                    ans += 1
                    break
        return ans

Java

class Solution {
    public int numRookCaptures(char[][] board) {
        int[] pos = find(board);
        int ans = 0, n = 8;
        int[][] dirs = new int[][] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
        for (int[] dir : dirs) {
            int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
            while (
                x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
                x += a;
                y += b;
                if (board[x][y] == 'p') {
                    ++ans;
                    break;
                }
            }
        }
        return ans;
    }

    private int[] find(char[][] board) {
        int n = 8;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (board[i][j] == 'R') {
                    return new int[] {i, j};
                }
            }
        }
        return null;
    }
}

C++

class Solution {
public:
    int numRookCaptures(vector<vector<char>>& board) {
        vector<int> pos = find(board);
        int ans = 0, n = 8;
        vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        for (auto& dir : dirs) {
            int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
            while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
                x += a;
                y += b;
                if (board[x][y] == 'p') {
                    ++ans;
                    break;
                }
            }
        }
        return ans;
    }

    vector<int> find(vector<vector<char>>& board) {
        int n = 8;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (board[i][j] == 'R') {
                    return {i, j};
                }
            }
        }
        return {};
    }
};

Go

func numRookCaptures(board [][]byte) int {
	n := 8

	find := func() []int {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if board[i][j] == 'R' {
					return []int{i, j}
				}
			}
		}
		return []int{}
	}

	pos := find()
	ans := 0
	dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
	for _, dir := range dirs {
		x, y, a, b := pos[0], pos[1], dir[0], dir[1]
		for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != 'B' {
			x += a
			y += b
			if board[x][y] == 'p' {
				ans++
				break
			}
		}
	}
	return ans
}

...

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/BuYh/leetcode.git
git@gitee.com:BuYh/leetcode.git
BuYh
leetcode
leetcode
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891