其他分享
首页 > 其他分享> > LeetCode:999.Available Captures for Rook(车的可用捕获量)

LeetCode:999.Available Captures for Rook(车的可用捕获量)

作者:互联网

999.Available Captures for Rook(车的可用捕获量)

Description

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

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

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

题目链接:https://leetcode.com/problems/available-captures-for-rook/
个人主页:http://redtongue.cn or https://redtongue.github.io/

Difficulty: easy

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:

分析

参考代码

class Solution(object):
def numRookCaptures(self, board):
    row,col=0,0
    for i in range(8):
        for j in range(8):
            if(board[i][j]=='R'):
                row=i
                col=j
                break
    Sum=0
    for i in range(row-1,-1,-1):
        if(board[i][col]=="B"):
            break
        if(board[i][col]=="p"):
            Sum+=1
            break
    for i in range(row+1,8):
        if(board[i][col]=="B"):
            break
        if(board[i][col]=="p"):
            Sum+=1
            break
    for j in range(col-1,-1,-1):
        if(board[row][j]=="B"):
            break
        if(board[row][j]=="p"):
            Sum+=1
            break
    for j in range(col+1,8):
        if(board[row][j]=="B"):
            break
        if(board[row][j]=="p"):
            Sum+=1
            break
    return Sum

标签:Available,Rook,Sum,break,rook,捕获量,board,col,row
来源: https://blog.csdn.net/redtongue/article/details/87908376