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 == 8board[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日 ...
 
随机推荐
- 把java程序作为windows服务运行
			
参考: https://www.jianshu.com/p/fc9e4ea61e13 https://blog.csdn.net/qq_28566071/article/details/8088250 ...
 - git 一个分支完全覆盖另一个分支
			
1,git push origin develop:master -f就可以把本地的develop分支强制(-f)推送到远程master 2,git checkout master // 切换到旧的分 ...
 - excel 设置的函数在打开的时候不会自动执行
			
excel中设置了个today的函数,显示今天的日期,结果不执行. 解决方案:打开该excel,选择File-->Options ,在弹出的框框中选择Formulas,在主界面的Calcula ...
 - spring mvc 注解整理(一)
			
@Controller和@RestController: RestController = @ResponseBody + @Controller 所有返回都是json类型,无法跳转到jsp页面,但 ...
 - 刘志梅  201771010115 《面向对象程序设计(java)》 第八周学习总结
			
实验六 接口的定义与使用 实验时间 2018-10-18 1.实验目的与要求 (1) 接口定义:接口不是类,而是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义:由常量和一组抽象方法组成:接 ...
 - beautifulSoup《转》
			
beautifulSoup基本用法及find选择器 总结来源于官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.h ...
 - NodeJS学习之win10安装与sublime配置
			
Window 上安装Node.js Node.js安装包及源码下载地址为:https://nodejs.org/en/download/ 下载安装就行了,安装node会同时安装npm. sublime ...
 - logback的使用和logback.xml详解,在Spring项目中使用log打印日志
			
logback的使用和logback.xml详解 一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分 ...
 - java 集合是否有序
			
参考:https://www.cnblogs.com/hoobey/p/5914226.html
 - Java语法 [HelloWorld]
			
程序代码: public class lqx {// AAAAANBBBBCKJKSLJIOQL/*请手打哦!*/ public static void main (String[] args) { ...