zoukankan      html  css  js  c++  java
  • 【leetcode】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.

    Example 1:

    Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
    Output: 3
    Explanation: 
    In this example the rook is able to capture all the pawns.
    

    Example 2:

    Input: [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
    Output: 0
    Explanation: 
    Bishops are blocking the rook to capture any pawn.
    

    Example 3:

    Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
    Output: 3
    Explanation: 
    The rook can capture the pawns at positions b5, d6 and f5.
    

    Note:

    1. board.length == board[i].length == 8
    2. board[i][j] is either 'R''.''B', or 'p'
    3. There is exactly one cell with board[i][j] == 'R'

    解题思路:非常简单的题目,遍历board把'R'找到,然后以这个点为基础往上下左右四个方向遍历,遇到非'.'则停止该方向的遍历,如果元素是'p',则可以捕获的猎物+1。代码随便写写了,也没有去优化美观一下。

    代码如下:

    class Solution(object):
        def numRookCaptures(self, board):
            """
            :type board: List[List[str]]
            :rtype: int
            """
            rx,ry = 0,0
            for i in range(len(board)):
                for j in range(len(board[i])):
                    if board[i][j] == 'R':
                        rx = i
                        ry = j
                        break
            res = 0
            for i in range(ry-1,-1,-1):
                if board[rx][i] == '.':
                    continue
                elif board[rx][i] == 'p':
                    res += 1
                    break
                else:
                    break
    
            for i in range(ry+1,len(board[rx])):
                if board[rx][i] == '.':
                    continue
                elif board[rx][i] == 'p':
                    res += 1
                    break
                else:
                    break
    
            for i in range(rx-1,-1,-1):
                if board[i][ry] == '.':
                    continue
                elif board[i][ry] == 'p':
                    res += 1
                    break
                else:
                    break
    
            for i in range(rx+1,len(board)):
                if board[i][ry] == '.':
                    continue
                elif board[i][ry] == 'p':
                    res += 1
                    break
                else:
                    break
    
            return res
  • 相关阅读:
    CCF总结
    句柄的定义
    ubuntu16.04下安装和使用Kdevelop的技巧
    找不到ros相关包的解决方法
    用kdevelop构建ROS程序包
    Linux给软件添加卓面快捷方式
    工程代码结构说明——哈工大编译原理课程(五)
    符号表管理——哈工大编译原理课程(四)
    语义分析和中间代码生成——哈工大编译原理课程(三)
    语法分析器——哈工大编译原理课程(二)
  • 原文地址:https://www.cnblogs.com/seyjs/p/10432115.html
Copyright © 2011-2022 走看看