【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 ...
随机推荐
- 超级好用的excel导出方法,比phpexcel快n倍,并且无乱码
public function exportToExcel($filename, $tileArray=[], $dataArray=[]){ ini_set('memory_limit','512M ...
- Linux启动与停止Tomcat
停止Tomcat: cd 切换到Tomcat的bin目录下,关闭命令:[root@localhost bin]# ./shutdown.sh 检查tomcat是否已关闭,检查命令:[root@loca ...
- webapp接口安全设计思路
在做webqq或者说app开发的时候,免不了会有接口是有权限的(如查询用户敏感信息等),这时接口安全设计思路就非常重要了. 简单一点,在APP中保存登录数据,每次调用接口时传输 程序员总能给自己找到偷 ...
- flume 测试 hive sink
测试flume,将数据送到hive表中,首先建表. create table order_flume( order_id string, user_id string, eval_set string ...
- K8S组件
Master 组件 Master组件提供集群的管理控制中心.Master组件可以在集群中任何节点上运行.但是为了简单起见,通常在一台VM/机器上启动所有Master组件,并且不会在此VM/机器上运行用 ...
- Monkey框架(基础知识篇) - monkey启动与参数介绍
一.monkey启动 直接PC启动:> adb shell monkey [options] <count> shell 端启动:> adb shell >monkey ...
- Java_jdbc 基础笔记之十四 数据库连接(元数据)数据库信息及连接信息
public class MetaDatatest { /** * DatabaseMetaData 是描述 数据库的元数据对象 可以由Connection得到 */ @Test public voi ...
- Nginx Windows版安装及域名绑定
1.到 http://nginx.org/en/download.html 下载一个稳定版本1.16.1 2.解压,放到C盘根目录下,如C:\nginx-1.16.1,此时双击nginx.exe就启动 ...
- 【Nginx】Nginx服务器配置调优
1.Nginx服务器配置调优 .设置nginx全局参数 vi /usr/local/nginx/conf/nginx.conf #编辑 worker_processes ; # 工作进程数,为CPU的 ...
- ElementUi tree 指定节点是否显示复选框
场景:树的内容是省份下面的城市有酒店 需求:只能多选酒店(为了删除它们),至于为啥不能选省份或者城市更加灵活的去删除相应酒店,这你得去问后台0.0,他只弄了根据酒店id去删除.嗯,连创建酒店的时候级联 ...