【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 ...
随机推荐
- pgloader 学习(八) pg 2 pg 简单demo
pg 数据到pg 数据的迁移,同时支持名称的变更 环境准备 docker-compose文件 内容偏多可以忽略部分 version: "3" services: pgloader- ...
- 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛
Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...
- 用jquery做一个带导航的名单列表
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- mysql 显示表结构
例子 mysql> show columns from table1; +------------+------------------+------+-----+---------+----- ...
- mapreduce中reduce没有执行
hadoop执行mapreduce过程reduce不执行原因 1.如果你的map过程中没有context.write()是不执行reduce过程的:2.如果你的map过程中context.write( ...
- GitHub如何删除一个代码仓库
进入GitHub之后,点击“your repositories”查看所有的代码仓库. 在代码仓库中选择一个需要删除的,进入其详情页 在详情页中找到“setting”设置,下拉至最后可以看到删除选项. ...
- python创建缩略图和选择轮廓效果
# -*- encoding:utf-8 -*- ''' 改变颜色 --- 颜色反转''' from PIL import Image nest = Image.open("D:\\tk.j ...
- 【E2EL5】A Year in Computer Vision中关于图像增强系列部分
http://www.themtank.org/a-year-in-computer-vision 部分中文翻译汇总:https://blog.csdn.net/chengyq116/article/ ...
- word/wps 制作下拉列表
准备: 1.数据页 2.项目名称sheet 3.问题类型sheet 开始制作: 数据 --- 有效性 --- 允许“序列” --- 来源 -- 其他sheet页“单元格”选择范围 回车.确定 即可
- docker安装并运行elasticsearch
拉取镜像: [mall@VM_0_7_centos ~]$ [sudo] password for mall: : Pulling from library/elasticsearch 256b176 ...