返回介绍

solution / 0900-0999 / 0999.Available Captures for Rook / README

发布于 2024-06-17 01:03:32 字数 7832 浏览 0 评论 0 收藏 0

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'

解法

方法一:模拟

我们先遍历棋盘,找到车的位置 $(x, y)$,然后从 $(x, y)$ 出发,向上下左右四个方向遍历:

  • 如果遇到象或者边界,那么该方向停止遍历;
  • 如果遇到卒,那么答案加一,然后该方向停止遍历;
  • 否则,继续遍历。

遍历完四个方向后,即可得到答案。

时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是棋盘的行数和列数,本题中 $m = n = 8$。空间复杂度 $O(1)$。

class Solution:
  def numRookCaptures(self, board: List[List[str]]) -> int:
    ans = 0
    dirs = (-1, 0, 1, 0, -1)
    for i in range(8):
      for j in range(8):
        if board[i][j] == "R":
          for a, b in pairwise(dirs):
            x, y = i, j
            while 0 <= x + a < 8 and 0 <= y + b < 8:
              x, y = x + a, y + b
              if board[x][y] == "p":
                ans += 1
                break
              if board[x][y] == "B":
                break
    return ans
class Solution {
  public int numRookCaptures(char[][] board) {
    int ans = 0;
    int[] dirs = {-1, 0, 1, 0, -1};
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        if (board[i][j] == 'R') {
          for (int k = 0; k < 4; ++k) {
            int x = i, y = j;
            int a = dirs[k], b = dirs[k + 1];
            while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8
              && board[x + a][y + b] != 'B') {
              x += a;
              y += b;
              if (board[x][y] == 'p') {
                ++ans;
                break;
              }
            }
          }
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int numRookCaptures(vector<vector<char>>& board) {
    int ans = 0;
    int dirs[5] = {-1, 0, 1, 0, -1};
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        if (board[i][j] == 'R') {
          for (int k = 0; k < 4; ++k) {
            int x = i, y = j;
            int a = dirs[k], b = dirs[k + 1];
            while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
              x += a;
              y += b;
              if (board[x][y] == 'p') {
                ++ans;
                break;
              }
            }
          }
        }
      }
    }
    return ans;
  }
};
func numRookCaptures(board [][]byte) (ans int) {
  dirs := [5]int{-1, 0, 1, 0, -1}
  for i := 0; i < 8; i++ {
    for j := 0; j < 8; j++ {
      if board[i][j] == 'R' {
        for k := 0; k < 4; k++ {
          x, y := i, j
          a, b := dirs[k], dirs[k+1]
          for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' {
            x, y = x+a, y+b
            if board[x][y] == 'p' {
              ans++
              break
            }
          }
        }
      }
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文