package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: NumRookCaptures
* @Author: xiaof
* @Description: 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.
*
* Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],
* [".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],
* [".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
* Output: 3
* Explanation:
* In this example the rook is able to capture all the pawns.
*
* 在一个国际象棋的棋盘上,有一个白车(R),有若干白象(B)、黑卒(p),其余是空白(.),问这个白车在只移动一次的情况下,能吃掉哪几个黑卒
*
* @Date: 2019/7/4 9:20
* @Version: 1.0
*/
public class NumRookCaptures { public int solution(char[][] board) {
//因为白车是横向和纵向都可以移动全部位置,那么我们只需要判断四个方向就可以知道能吃几个了
//第一步肯定是定位到白车R
int row = 0, column = 0;
for(int i = 0; i < board.length; ++i) {
for(int j = 0; j < board[i].length; ++j) {
if(board[i][j] == 'R') {
row = i;
column = j;
break;
}
}
} //然后根据白车的位置横向或纵向比那里获取黑卒个数
int result = 0;
//横
for(int index1 = column - 1, index2 = column + 1; index1 >= 0 || index2 < board[row].length; ++index2, --index1) {
if(index1 >= 0 && board[row][index1] == 'p') {
result++;
//并且只能吃一次
index1 = -1;
} else if (index1 >= 0 && board[row][index1] == 'B') {
index1 = -1; //如果遇到B,白象那么就停下
} if(index2 < board[row].length && board[row][index2] == 'p') {
result++;
index2 = board[row].length;
} else if (index2 < board[row].length && board[row][index2] == 'B') {
index2 = board[row].length; //如果遇到B,白象那么就停下
}
} //纵向
for(int index1 = row - 1, index2 = row + 1; index1 >= 0 || index2 < board.length; ++index2, --index1) {
if(index1 >= 0 && board[index1][column] == 'p') {
result++;
//并且只能吃一次
index1 = -1;
} else if (index1 >= 0 && board[index1][column] == 'B') {
index1 = -1; //如果遇到B,白象那么就停下
} if(index2 < board.length && board[index2][column] == 'p') {
result++;
index2 = board[row].length;
} else if (index2 < board.length && board[index2][column] == 'B') {
index2 = board.length; //如果遇到B,白象那么就停下
}
} return result;
} public static void main(String args[]) {
char A1[][] = {{'.','.','.','.','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'p','p','.','R','.','p','B','.'},{'.','.','.','.','.','.','.','.'},{'.','.','.','B','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','.','.','.','.','.'}};
NumRookCaptures fuc = new NumRookCaptures();
System.out.println(fuc.solution(A1));
} }

【LEETCODE】46、999. Available Captures for Rook的更多相关文章

  1. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  2. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  6. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  7. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  8. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  9. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

随机推荐

  1. 3-ESP8266 SDK开发基础入门篇--点亮一个灯

    https://www.cnblogs.com/yangfengwu/p/11072834.html 所有的源码 https://gitee.com/yang456/Learn8266SDKDevel ...

  2. C Primer Plus--结构和其他数据类型(2)

    目录 枚举类型 enumerated type 枚举默认值 为枚举指定值 命名空间 namespace typedef关键字 * () []修饰符 typedef与这三个运算符结合 函数与指针 函数指 ...

  3. mysql 获取学生个人科目平均分

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  4. 多维矩阵转一维数组(c++)【转载】

    在由二维矩阵转为一维数组时,我们有两种方式:以列为主和以行为主. 以列为主的二维矩阵转为一维数组时,转换公式为: index=column+row×行数 以行为主的二维矩阵转为一维数组时,转换公式为: ...

  5. windows 共享网络

    windows 共享网络 今天单位的网络突然断了,光猫LOS亮红灯,宽带报修.等了半天还没来,下面科室要上报资料,急着用网, 通过windows的共享网络+无线网卡暂时用我的手机流量. 找了一台电脑插 ...

  6. 美团-2019Q2述职总结

    述职要求: 产品对平台化的规划并不清晰:内部因素:对SaaS平台的理解不够深刻: 对公司相关脚手架,服务搭建相关需要注意的点,有更深入的认识.对做系统服务的关注点有了更深入的理解. 功能权限的话: Q ...

  7. assert(0)的作用

    捕捉逻辑错误.可以在程序逻辑必须为真的条件上设置断言.除非发生逻辑错误,否则断言对程序无任何影响.即预防性的错误检查,在认为不可能的执行到的情况下加一句ASSERT(0),如果运行到此,代码逻辑或条件 ...

  8. Spark安装(standalone)

    文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...

  9. javascript submit() is not a function

    <script> window.onload = function(){ document.getElementById('form').submit(); } </script&g ...

  10. nodeJs实现文件上传,下载,删除

    转:https://blog.csdn.net/qq_36228442/article/details/81709272 一.简介 本文介绍了nodeJs+express框架下,用multer中间件实 ...