【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:
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'
解题思路:非常简单的题目,遍历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的更多相关文章
- 【LeetCode】999. Available Captures for Rook 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四方向搜索 日期 题目地址:https://leetc ...
- 【LeetCode】999. Available Captures for Rook 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【LEETCODE】46、999. Available Captures for Rook
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】746. 使用最小花费爬楼梯
使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- vue中使用canvas绘制签名
不多说,上代码: <template> <div class="sign-canvas"> <canvas ...
- nuget cli 打包发包
微软官网打包说明:https://docs.microsoft.com/zh-cn/nuget/quickstart/create-and-publish-a-package-using-visual ...
- 【优化】Java开发中注意内存问题,影响JVM
1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 第一,控制资源的使用,通过线程同步来控 ...
- Ext js-01 -helloworld
一.下载ext: 登陆这个网址 https://www.sencha.com/products/evaluate/ 下载下来解压后如下:安装cmd程序 二.开始helloworld 新建一个idea ...
- 使用mysql应该注意的细节
一.表及字段的命名规范 1.可读性原则 使用大写和小写来格式化的库对象名字以获得良好的可读性. 例如:使用CustAdress而不是custaddress来提高可读性.(这里注意有些DBMS系统对表名 ...
- 2016年Esri技术公开课全年资料分享
大家好,2016年的公开课活动在上周全部结束,感谢大家的支持. 2016年的公开课共进行20期,共有24位讲师参与,公开课视频播放.课件下载次数累计超10万次,在这里衷心的感谢大家的积极参与和分享精神 ...
- php关键字static使用
php中static关键字使用: 情景1:静态变量 使用static关键字定义静态变量 静态变量:只存在于函数作用域内,也就是说,静态变量只存活在栈中.一般的函数内变量在函数结束后会释放,比如局部变量 ...
- Git-学习开源代码的技巧
从最初提交开始学习每次提交的代码 https://stackoverflow.com/questions/5630110/how-to-read-source-code-using-git 很久以前就 ...
- QTP--启动IE浏览器的三种方式
第一种方式 创建浏览器对象模式 如果提示无法创建对象时需要先打开对象. Set ie = CreateObject("InternetExplorer.Application" ...
- Cocos2d-x之物理引擎
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在很多的游戏设计中一般都会涉及和模拟到真实的物理世界.然而游戏中模拟真实世界的物理会很复杂.使用已经写好的物理引擎会用很大的帮助和便利. ...