zoukankan      html  css  js  c++  java
  • 999.Available Capture for rook

    999.Available Captures for Rook
     
    车的可用捕获量
    在一个8 × 8的棋盘上,有一个白色车(rook),也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符“R”,“.”,“B”和“p”给出。大写字符表示白棋,小写字符表示黑棋。</br>
    车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。<br>返回车能够在一次移动中捕获到的卒的数量。
    示例:

    输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
     
    输出:3
     
    解释:在本例中,车能够捕获所有的卒。
     
    Solution 1:
    def numRockCaptures(board):
        """
        :type board: List[List[str]]
        :rtype: int
        """
        result = 0
        index = 0
        for i in board:
            if 'R' in i:
                row = ''.join(z for z in i if z != '.')
                if 'Rp' in row:
                    result += 1
                if 'pR' in row:
                    result += 1
                index = i.index("R")
                break
        col = ''.join(board[i][index] for i in range(8) if board[i][index] != '.')
        if 'Rp' in col:
            result += 1
        if 'pR' in col: 
            result += 1
    
        return result
    
    if __name__ == '__main__':
        """
        找到车的位置,然后将车所在的list转为str
        判断str中是否存在'Rp'和'pR'
        然后再获取车所在列的list,将它转为str
        判断str中是否存在'Rp'和'pR'
        """
        # test case
        board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
        result = numRockCaptures(board)
        print(result)
    

    Solution 2:

    def numRockCaptures(board):
        """
        :type board: List[List(str)]
        :reype: int
        """
        for i in range(8):
            for j in range(8):
                if board[i][j] == 'R':
                    x0, y0 = i, j
        
        res = 0
        for i, j in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            x, y = x0 + i, y0 + j
            while 0 <= x < 8 and 0 <= y < 8:
                if board[x][y] == 'p':
                    res += 1
                    break
                if board[x][y] != '.':
                    break
                x, y = x + i, y + j
        return res
    
    if __name__ == '__main__':
        """
        先找到车R的位置
        然后依次从四个方向去遍历
        """
        # test case
        board = [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
        print(numRockCaptures(board))
    

      

  • 相关阅读:
    (黑马十次方项目day06)@ConfigurationProperties报错的解决方式
    (黑马十次方项目day04)An attempt was made to call a method that does not exist. The attempt was made from the following location:
    (黑马十次方项目day02)使用map接收form表单的参数
    (黑马十次方项目day02)IDEA在方法之间添加分隔符及开启Run Dashboard管理
    (黑马十次方项目day01)spring-boot-starter-parent 包maven依赖报错
    (黑马十次方项目day01)从PDF文件中复制代码到pom文件中project报错
    ER图学习
    java 8 函数式库Vavr功能
    Guava Cache
    UML学习
  • 原文地址:https://www.cnblogs.com/mrjoker-lzh/p/10560576.html
Copyright © 2011-2022 走看看