开发者

go语言LeetCode题解999可以被一步捕获的棋子数

开发者 https://www.devze.com 2022-12-29 10:54 出处:网络 作者: 刘09k11
目录题目描述思路分析AC 代码题目描述 999. 可以被一步捕获的棋子数 - 力扣(LeetCode)
目录
  • 题目描述
  • 思路分析
  • AC 代码

题目描述

999. 可以被一步捕获的棋子数 - 力扣(LeetCode)

在一个 8 x 8 的棋盘上,有一个白色的车(Rook),用字符 'R' 表示。棋盘上还可能存在空方块,白色的象(Bishop)以及黑色的卒(pawn),分别用字符 '.''B''p' 表示。不难看出,大写字符表示的是白编程客栈棋,小写字符表示的是黑棋。

车按国际象棋中的规则移动。东,西,南,北四个基本方向任选其一,然后一开发者_JAVA学习直向选定的方向移动,直到满足下列四个条件之一:

  • 棋手选择主动停下来。
  • 棋子因到达棋盘的边缘而停下。
  • 棋子移动到某一方格来捕获位于该方格上敌方(黑色)的卒,停在该方格内。
  • 车不能进入/越过已经放有其他友方棋子(白色的象)的方格,停在友方棋子前。

你现在可以控制车移动一次,请你统计有多少敌方的卒处于你的捕获范围内(即,可以被一步捕获的棋子数)。

示例 1:

go语言LeetCode题解999可以被一步捕获的棋子数

输入:[[".",".",".",&编程quot;.",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".&quopythont;,".","."]]

输出:3

解释:

在本例中,车能够捕获所有的卒。

示例 2:

go语言LeetCode题解999可以被一步捕获的棋子数

输入:[[".",".",".",".",".",".",".","."],[".","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:

go语言LeetCode题解999可以被一步捕获的棋子数

输入:[[".",".",".",".",".",".",".","."],[".",".",".","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'

思路分析

这道题首先要理解题意

  • 如果没有阻挡,车可以无限移动,除非自己停止
  • 遇到象,停止。停止的意思是不能向前,但可以向后
  • 遇到边,停止。停止的意思是不能向前,但可以向后
  • 遇到卒,吃掉,然后在这个方向上必须停止。

解题方法,先整理

  • 去掉没用的信息
  • 把二维问题转为一维问题。

AC 代码

/**
 * @param {character[][javascript]} board
 * @return {number}
 */
var numRookCaptures = function (board) {
  let count = 0
  let info = []
  for (let i = 0; i < 8; i++) {
    let item = []
    for (let j = 0; j < 8; j++) {
      if ('.' !== board[i][j]) {
        item.push(board[i][j])
      }
    }
    item.length > 0 && info.push(item)
  }
  for (let j = 0; j < 8; j++) {
    let item = []
    for (let i = 0; i < 8; i++) {
      if ('.' !== board[i][j]) {
        item.push(board[i][j])
      }
    }
    item.length > 0 && info.push(item)
  }
  //整理好python后的info是个一维数组
  for (let item of info) {
    let index = item.indexOf('R')
    if (index < 0) continue
    let i = index
    while (i--) {
      if (item[i] === 'B') break;
      if (item[i] === 'p') {
        count++
        break
      }
    }
    i = index + 1
    while (i < item.length) {
      if (item[i] === 'B') break;
      if (item[i] === 'p') {
        count++
        break
      }
      i++
    }
  }
  return count
};

以上就是go语言LeetCode题解999可以被一步捕获的棋子数的详细内容,更多关于go 一步捕获的棋子数的资料请关注我们其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号