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:

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

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

Output: 3
Explanation: 
In this example the rook is able to capture all the pawns.

Example 2:

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

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:

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

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:

  • board.length == board[i].length == 8

  • board[i][j] is either ‘R’, ‘.’, ‘B’, or ‘p’

  • There is exactly one cell with board[i][j] == ‘R’

分析

  • 找到白色车(R)的位置,分别向八个方向寻找卒(p);

  • 若某个方向上碰到某个象(B)或者棋盘的边缘,则停止该方向的寻找;

  • 若找到卒,Sum加一;

  • 返回Sum。

参考代码

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
上一篇:k8s 集群部署rook-ceph存储系统及使用


下一篇:999. Available Captures for Rook