【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 ...
随机推荐
- vue-cli3 ios10白屏问题解决思路
在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*' ...
- 洛谷 P4779 【模板】单源最短路径(标准版) 题解
P4779 [模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 100 ...
- log4j 1.2 配置和使用简述
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/log4j_properties_simple_introduct ...
- 如何更改sdk版本
- python设计模式---绪论
1.程序只是一个工具,只知道使用工具就有价值的时代正在过去:现在对工作质量.开发速度及完美程度都很重要了.当前主要的问题是对工具的充分利用,在生活的方方面面,简单任务之所以简单是由于这些任务不需要特殊 ...
- Freemarker的简单demo
第一步.导入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- vscode插件开发之如何玩转vscode命令
这里以插件开发为例,VsCode之所以那么强大是因为它背后有千千万万的开发者们为其开发大量功能插件,WordPress同理. 那么如何玩转VsCode命令呢(以插件开发为例)? 官方文档必不可少 ht ...
- [Beta]第五次 Scrum Meeting
[Beta]第五次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/13 22:00 30min 大运村公寓6F楼道 附Github仓库:WEDO 例会照片 (一人上 ...
- 工具系列 | 使用Lodop进行WEB打印程序开发
Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又可用程序代码直接实现 复杂打印.控件功能强大,却简单易用,所有调用如同JavaScript扩展语句. WEB套打可 ...
- 《精通CSS第3版》(6)内容布局(定位+水平布局)