【LEETCODE】46、999. Available Captures for Rook
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的更多相关文章
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
随机推荐
- vlang module 使用
vlang 支持module,概念以及使用类似rust 以及golang 的gopath(从当前的文档以及使用来说),但是还不完整 以及是够用,但是有问题 v module 试用 项目结构 ├── ...
- kafka-python 1.4.6 版本触发的一个 rebalance 问题
在使用了最新版的 kafka-python 1.4.6 在 broker 对 topic 进行默认配置的情况下报出类似错误 CommitFailedError CommitFailedError: C ...
- IDEA中用mybatis插件生成逆向工程
目录 maven项目 在resources目录下新建generatorConfig.xml文件 在resources目录下新建config.properties文件 运行 maven项目 <?x ...
- .bat批处理命令之设置关机倒计时脚本
@ECHO off REM 不显示后续命令行及当前命令行 TITLE Shutdown countdown REM 设置脚本标题 COLOR 0A REM 设置脚本 背景色为黑色 前景色为淡绿色 :s ...
- shell 修改文件所有者
chown 用户名 文件名 -R
- CSS系列之后代选择器、子选择器和相邻兄弟选择器
后代选择器比子选择器的范围大,包含子选择器,且包含子选择器的“子孙”选择器,后代选择器使用"空格"符号间隔选择器 子选择器:子选择器只是父选择器的一级子元素,使用"> ...
- Vue 自定义编译打包路径
在 vue.config.js 文件下添加 outputDir 配置项: module.exports = { outputDir:"my_target_direct", // o ...
- 剑指offer:数组中的逆序对
题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%100 ...
- 第2课第5节_Java面向对象编程_异常_P【学习笔记】
摘要:韦东山android视频学习笔记 java的异常处理的原则如下: 1.我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问 ...
- MySQL8.0项目启动遇到的问题
写在前面 看到jeecg论坛更新了jeecg-boot版本, 比较新颖的技术都有, down代码, 执行sql脚本, 起项目, 本来是一气呵成的事儿遇到了两个问题, 做个记录. 环境: IDEA201 ...