3种解法 - 得到国际象棋中车的可用捕获量


题目

在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“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
解释:
象阻止了车捕获任何卒。

提示:

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        
发布了290 篇原创文章 · 获赞 45 · 访问量 26万+
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览