Available Captures for Rook LT999
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'
Idea 1. walking 4 directions until eaither hit the edge or 'B' or 'p'.
Time complexity: O(2n), here n = 8
Space complexity: O(1)
class Solution {
private int walking(char[][] board, int x, int y) {
int N = 8;
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int count = 0;
for(int[] dir: dirs) {
for(int nextX = x + dir[0], nextY = y + dir[1];
nextX >= 0 && nextX < N && nextY >= 0 && nextY < N
&& board[nextX][nextY] != 'B';
nextX += dir[0], nextY += dir[1]) {
if(board[nextX][nextY] == 'p') {
++count;
break;
}
} }
return count;
} public int numRookCaptures(char[][] board) {
for(int i = 0; i < board.length; ++i) {
for(int j = 0; j < board[i].length; ++j) {
if(board[i][j] == 'R') {
return walking(board, i, j);
}
}
} return 0;
}
}
Available Captures for Rook LT999的更多相关文章
- 【LEETCODE】46、999. Available Captures for Rook
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- [Swift]LeetCode999. 车的可用捕获量 | Available Captures for Rook
在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 “R”,“.”,“B” 和 “p” 给出.大写字符表示白棋,小写 ...
- 【leetcode】999. Available Captures for Rook
题目如下: On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bisho ...
- 【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 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...
- 【LeetCode】Available Captures for Rook(车的可用捕获量)
这道题是LeetCode里的第999道题. 题目叙述: 在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 &quo ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
随机推荐
- easyui layout布局的属性说明
layout布局的属性说明: 名称 类型 描述 默认值 fit boolean 当设置为 true 时,就设置布局(layout)的尺寸适应它的父容器.当在 'body' 标签上创建布局(layout ...
- 【剑指offer】广度优先遍历二叉树
问题:从上往下打印出二叉树的每个节点,同层节点从左至右打印. *思路:先用队列存放树的根结点.每次出队一个结点,将结点非空的左右孩子分别入队.重复此过程,直到队列为空. import java.uti ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PowerDesiger 生成C#实体类,字段转变成大小写方法
1.打开编辑生成C#实体类的PD脚本 2..L: 表示全部转小写,.U: 表示全部转成大写.
- python网页爬虫开发之六-Selenium使用
chromedriver禁用图片,禁用js,切换UA selenium 模拟chrome浏览器,此时就是一个真实的浏览器,一个浏览器该加载的该渲染的它都加载都渲染,所以爬取网页的速度很慢.如果可以不加 ...
- Activiti的25张表
Activiti流程引擎的运行,背后需要数据库的25张表支持,这25张表, 主要是在流程运行过程中,记录存储一些参与流程的用户主体,以及流程定义的存储,流程执行时候的一些信息,以及流程的历史信息等. ...
- Vue非父子组件之间的传值
1.新建一个js文件 然后引入vue 实例化vue 最后暴露这个实例:实例化Vue对象的时候名称要小写,大写控制台报错,我也不知道什么原因: 2.在要广播的地方引入刚才定义的实例: 3通过VueEm ...
- python中定义的颜色
平时学习工作中,我们经常会接触到一些大佬写的Python实用工具,运行起来总会显示出五颜六色的背景,相关的定义在matplotlib模块中,为方便使用,这里给大家展示一下在这个模块中都定义了哪些选颜色 ...
- GC(垃圾处理机制)面试题
1: 详细解释Java中垃圾收集的主要流程. 垃圾收集主要有两种形式:手工.自动 自动会不定期进行回收,以释放无用的空间 手工调用的是System类中的gc()方法,此方法实际上调用的是Runtim ...
- 定位JVM内存溢出问题思路总结
JVM的内存溢出问题,是个常见而有时候有非常难以定位的问题.定位内存溢出问题常见方法有很多,但是其实很多情况下可供你选择的有效手段非常有限.很多方法在一些实际场景下没有实用价值.这里总结下我的一些定位 ...