ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-02-24 21:49:19  阅读:334  来源: 互联网

标签:Available Rook Sum break rook 捕获量 board col row


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:

  • 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

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有