题目如下:

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

【leetcode】999. Available Captures for Rook的更多相关文章

  1. 【LeetCode】999. Available Captures for Rook 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四方向搜索 日期 题目地址:https://leetc ...

  2. 【LeetCode】999. Available Captures for Rook 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  3. 【LEETCODE】46、999. Available Captures for Rook

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 【LeetCode】746. 使用最小花费爬楼梯

    使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...

  5. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Oracle11g新建用户及用户表空间

    /* 建立数据表空间 */CREATE TABLESPACE SP_TAB DATAFILE '/u01/app/oracle/oradata/orcl/tab1_1.dbf' size 1024M ...

  2. 组合的输出(回溯、dfs)

    问题 O: [回溯法]组合的输出 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r ...

  3. 使用kindeditor直接粘贴本地图片或者是qq截图

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...

  4. A Bug's Life - poj2492

    Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  5. Flutter样式和布局控件简析(二)

    开始 继续接着分析Flutter相关的样式和布局控件,但是这次内容难度感觉比较高,怕有分析不到位的地方,所以这次仅仅当做一个参考,大家最好可以自己阅读一下代码,应该会有更深的体会. Sliver布局 ...

  6. css > 的写法 html

    .userInfo-view .info .name::after { content: " "; display: inline-block; height: 12rpx; wi ...

  7. 广播接收者实现IP拨号

    广播接收者实现IP拨号 效果图: 实现的功能就是自动监听我们要拨打的号码,在我们拨打的号码前加上179521 分析: 1.敲个类来继承广播接收者 并且将从打电话应用位置获取的号码加上179521,并将 ...

  8. 强哥新周报SQL

    因为数据口径的更改,所以.强哥的SQL 比较好用.不会出麻烦. 总共有四个 日常记录下,好好看. -- 2019年4月核销新客 SELECT yzm2.consignee_phone AS `会员手机 ...

  9. CET-6 分频周计划生词筛选(Week 2)

    点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...

  10. 【Python-Django讲义】针对django的ppt讲义

    MCV思想: M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如一批统计数据你可以分别用柱状图.饼图来表示.C存在的目 ...