zoukankan      html  css  js  c++  java
  • 999. Available Captures for Rook

    On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, and black pawns.  These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.

    The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.  Also, rooks cannot move into the same square as other friendly bishops.

    Return the number of pawns the rook can capture in one move.

    8 * 8的棋盘,上面有R,.,B,p分别代表 rook, 空格子, bishops, black pawns, R的移动规则是直线移动无限格,但不能吃B,求R移动一次,能吃哪些p。

    我感觉问题描述上有点歧义,其实就是想问R的四个方向是否都有p且中间没有B阻挡。

    class Solution(object):
        def find_p(self, board, i, j, direction):
            while i < len(board) and i >= 0 and j < len(board[0]) and j >= 0 and board[i][j] != 'B':
                if board[i][j] == 'p':
                    return 1
                i += direction[0]
                j += direction[1]
            return 0
        
        def numRookCaptures(self, board):
            """
            :type board: List[List[str]]
            :rtype: int
            """
            ans = 0
            directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
            for i in range(len(board)):
                for j in range(len(board[i])):
                    if board[i][j] == 'R':
                        for direction in directions:
                            ans += self.find_p(board, i, j, direction)
                        break
            return ans
  • 相关阅读:
    windows(win10)下的mysql解压版安装
    微信和QQ网页授权登录
    图片前端重绘前端压缩和自动调整旋转
    时间的显示
    magento中文语言包的使用
    div垂直居中(js)
    centOS IP能ping通但是域名ping不通
    python3 使用pyinstaller打包exe 指定虚拟路径模块
    Flask 多app案例
    将excel的资产数据生成二维码图片
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13208960.html
Copyright © 2011-2022 走看看