美文网首页算法相关
3种解法 - 得到车的可用捕获量

3种解法 - 得到车的可用捕获量

作者: suoxd123 | 来源:发表于2020-03-26 10:29 被阅读0次

题目

在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。

车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。

返回车能够在一次移动中捕获到的卒的数量。

示例 1:


示例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'


解法一(行列查找)

思路:查找到白色车所在的坐标后,依次提取出对应的行列,然后在里面进行查找。

  1. 提取白色车所在的行列坐标
  2. 获取白色车对应行和列的值,组成新的列表
  3. 在两个列表中判断和计算
  • 时间复杂度:O(n2)
  • 空间复杂度:O(n)
class Solution:    
    def numRookCaptures(self, board: List[List[str]]) -> int:
        row,col , cnt = -1,-1,0
        lenRow, lenCol = len(board), len(board[0])
        #获取坐标位置
        for i in range(0,lenRow):
            if 'R' in board[i]:
                col = board[i].index('R')
                row = i
                break
        #子函数判断是否满足
        def checkP(bList, start, end, direct)-> int:
            for i in range(start,end,direct):
                if bList[i] == 'p':
                    return 1
                elif bList[i] == 'B':
                    return 0
            return 0
        #提取行列
        rowList = board[row]
        colList = [b[col] for b in board]
        #依次判断
        cnt += checkP(colList,row-1,-1,-1)
        cnt += checkP(colList,row+1,lenRow,1)
        cnt += checkP(rowList,col-1,-1,-1)
        cnt += checkP(rowList,col+1,lenCol,1)
        return cnt

解法二(递归查找)

思路:得到白色车所在行列后,以该位置为中心分别进行递归查找

  1. 提取白色车所在的行列坐标
  2. 使用递归在对应方向上持续寻找,直到边界
  3. 分别计算前后左右四个不同的方向
  • 时间复杂度:O(n2)
  • 空间复杂度:O(1)
class Solution:    
    def numRookCaptures(self, board: List[List[str]]) -> int:
        row,col , cnt = -1,-1,0
        lenRow, lenCol = len(board), len(board[0])
        #获取坐标位置
        for i in range(0,lenRow):
            if 'R' in board[i]:
                col = board[i].index('R')
                row = i
                break
        # 在一个方向上递归查找
        def checkP(rIdx, cIdx, rDelta, cDelta, lenList)-> int:
            if not( 0 <= rIdx < lenList and 0 <= cIdx < lenList):
                return 0
            if board[rIdx][cIdx] == 'p':
                return 1
            elif board[rIdx][cIdx] == 'B':
                return 0
            else :
                return checkP(rIdx + rDelta,cIdx + cDelta,rDelta,cDelta,lenList)
        # 四个方向分别计算
        cnt += checkP(row+1,col,1,0,lenRow)
        cnt += checkP(row,col+1,0,1,lenRow)
        cnt += checkP(row-1,col,-1,0,lenRow)
        cnt += checkP(row,col-1,0,-1,lenRow)
        return cnt
        

解法三(方向数组)

思路:基本原理跟解法二一致,使用方向数组进行循环查找

  1. 提取白色车所在的行列坐标
  2. 使用方向数组,在四个方向上分别进行循环,每次仅在一个方向上前进
  3. 累计四个方向的判断结果
  • 时间复杂度:O(n2)
  • 空间复杂度:O(1)
class Solution:    
    def numRookCaptures(self, board: List[List[str]]) -> int:
        row,col , cnt = -1,-1,0
        lenRow, lenCol = len(board), len(board[0])
        dirRow, dirCol = [1,0,-1,0],[0,1,0,-1]
        for i in range(0,lenRow):
            if 'R' in board[i]:
                col = board[i].index('R')
                row = i
                break
        for i in range(0,4):#4个方向
            rIdx, cIdx = row, col
            for j in range(0,lenRow):
                rIdx += dirRow[i]
                cIdx += dirCol[i]
                if not( 0 <= rIdx < lenRow and 0 <= cIdx < lenRow):
                    break
                if board[rIdx][cIdx] == 'p':
                    cnt += 1
                    break
                elif board[rIdx][cIdx] == 'B':
                    break
        return cnt        

相关文章

  • 3种解法 - 得到车的可用捕获量

    题目 在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(...

  • 车的可用捕获量

    题目 在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(...

  • 车的可用捕获量

    题目: 题目的理解: 题目很长,但是说的很情况,仔细看两遍应该就会明白是我们算什么值。简单描述就是:车能够吃到几个...

  • 999-车的可用捕获量

    车的可用捕获量 题目 在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bisho...

  • 999. 车的可用捕获量

    解题思路 解法一:暴力穷举 先找出车所在的行与列,然后分四个方向遍历寻找,分三种情况:1)要么当遇到象则退出本方向...

  • 打卡第26天:车的可用捕获量

    1. 题目描述 在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和...

  • 生产者与消费者问题

    在前一篇《实现进程互斥的几种方法》中最后提到了Peterson解法,还有硬件上的TSL解法,它们都是正确可用的方法...

  • leecode岛屿数量

    题目描述可用解法DFS 深度优先遍历BFS 广度优先遍历算法思路:下列代码用BFS,循环遍历输入的二维列表如果遇到...

  • 98. 验证二叉搜索树

    解法 递归解法 迭代解法

  • 104. 二叉树的最大深度

    解法 递归解法 层次遍历解法

网友评论

    本文标题:3种解法 - 得到车的可用捕获量

    本文链接:https://www.haomeiwen.com/subject/tspquhtx.html