这道题是LeetCode里的第999道题。

题目叙述:

在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。

车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。

返回车能够在一次移动中捕获到的卒的数量。

示例 1:

输入:[
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。

示例 2:

输入:[
[".",".",".",".",".",".",".","."],
[".","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",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
输出:0
解释:
象阻止了车捕获任何卒。

示例 3:

输入:[
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","p",".",".",".","."],
["p","p",".","R",".","p","B","."],
[".",".",".",".",".",".",".","."],
[".",".",".","B",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."]]
输出:3
解释:
车可以捕获位置 b5,d6 和 f5 的卒。

提示:

  1. board.length == board[i].length == 8
  2. board[i][j] 可以是 'R''.''B' 或 'p'
  3. 只有一个格子上存在 board[i][j] == 'R'

这道题很简单,首先我们要找到车的位置在哪,然后从车这个位置开始遍历它的上下左右方向的格子,判断是否为象或者卒。

代码如下:

class Solution {
public int numRookCaptures(char[][] board) {
int R_x=-1,R_y=-1;
int res=0;
//find R
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
if(board[i][j]=='R') {
R_x=i;R_y=j;break;
}
}
if(R_x>=0 && R_y>=0)break;
}
//up,down,left,right
for(int i=1;i+R_y<8;i++) {
if(board[R_x][i+R_y]=='B')break;
else if(board[R_x][i+R_y]=='p'){res++;break;}
}
for(int i=1;R_y-i>=0;i++) {
if(board[R_x][R_y-i]=='B')break;
else if(board[R_x][R_y-i]=='p') {res++;break;}
}
for(int i=1;R_x+i<8;i++) {
if(board[R_x+i][R_y]=='B')break;
else if(board[R_x+i][R_y]=='p') {res++;break;}
}
for(int i=1;R_x-i>=0;i++) {
if(board[R_x-i][R_y]=='B')break;
else if(board[R_x-i][R_y]=='p'){res++;break;}
}
return res;
}
}

提交结果:

个人总结:

这题数组题真的太简单了,被第1000题虐后来这里找找自信!

【LeetCode】Available Captures for Rook(车的可用捕获量)的更多相关文章

  1. Leetcode 999. 车的可用捕获量

    999. 车的可用捕获量  显示英文描述 我的提交返回竞赛   用户通过次数255 用户尝试次数260 通过次数255 提交次数357 题目难度Easy 在一个 8 x 8 的棋盘上,有一个白色车(r ...

  2. Java实现 LeetCode 999 车的可用捕获量(简单搜索)

    999. 车的可用捕获量 在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 "R"," ...

  3. [Swift]LeetCode999. 车的可用捕获量 | Available Captures for Rook

    在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 “R”,“.”,“B” 和 “p” 给出.大写字符表示白棋,小写 ...

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

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

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

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

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

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

  7. 【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 ...

  8. Leetcode 999. Available Captures for Rook

    class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...

  9. Available Captures for Rook LT999

    On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, an ...

随机推荐

  1. P1051 谁拿了最多奖学金——水题

    题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...

  2. Java 8新特性--Lambda表达式作为返回值

    lambda表达式作为方法的返回值:

  3. Android:Service通知Activity更新界面

    Android有四大组件,其中包括service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. 1.首先Activity调用Service 这个是比较基础的,它有两种 ...

  4. scrollviews page分页实现方式

    代码 buttonX = 0; buttonW = 50; buttonH = 20; margin = (self.view.width - 5 * buttonW) / 6; CGFloat ym ...

  5. WPF中做出一个QQ登陆界面

    Xaml: <Window x:Class="ChatSoftware.MainWindow" xmlns="http://schemas.microsoft.co ...

  6. 为什么JS是单线程?JS中的Event Loop(事件循环)?JS如何实现异步?setimeout?

    https://segmentfault.com/a/1190000012806637 https://www.jianshu.com/p/93d756db8c81 首先,请牢记2点: (1) JS是 ...

  7. 【page-monitor 前端自动化 下篇】 实践应用

    转载文章:来源(靠谱崔小拽) 通过page-diff的初步调研和源码分析,确定page-diff在前端自动化测试和监控方面做一些事情.本篇主要介绍下,page-diff在具体的实践中的一些应用 核心d ...

  8. w3 parse a url

     最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...

  9. Spring对注解(Annotation)处理【转】

    1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  10. java在线聊天项目 使用SWT快速制作登录窗口,可视化窗口Design 更换窗口默认皮肤(切换Swing自带的几种皮肤如矩形带圆角)

    SWT成功激活后 new一个JDialog 调整到Design视图 默认的视图模式是BorderLayout,无论你怎么拖拽,只能放到东西南北中的位置上 所以,我们把视图模式调整为AbsoluteLa ...