【LeetCode】999. Available Captures for Rook 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.
Example 1:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
In this example the rook is able to capture all the pawns.
Example 2:
Input: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0
Explanation:
Bishops are blocking the rook to capture any pawn.
Example 3:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation:
The rook can capture the pawns at positions b5, d6 and f5.
Note:
board.length == board[i].length == 8board[i][j]is either'R', '.', 'B', or 'p'- There is exactly one cell with
board[i][j] == 'R'
题目大意
国际象棋的棋盘上有一个白方的车,还有若干个白方的象,还有若干黑方的卒。车可以向四个方向行走,白色的象会挡住车的去路,问最多能吃掉多少个黑色的卒。
解题方法
四方向搜索
这个题和炸弹人的题目是一样的。
- 先找到白色的车的位置;
- 然后从该位置出发,向四个方向进行搜索,如果遇到边界或者白色的象那么停止搜索;遇到黑色的卒那么就把能吃的卒的数目加1,并且也要停止搜索。
C++代码如下:
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int N = 8;
int row = -1;
int col = -1;
bool findR = false;
for (int i = 0; i < N && !findR; ++i) {
for (int j = 0; j < N && !findR; ++j) {
if (board[i][j] == 'R') {
row = i;
col = j;
break;
}
}
}
int res = 0;
for (auto& dir : directions) {
int x = row;
int y = col;
while (true) {
x += dir[0];
y += dir[1];
if (x < 0 || x >= N || y < 0 || y >= N
|| board[x][y] == 'B') {
break;
}
if (board[x][y] == 'p') {
res ++;
break;
}
}
}
return res;
}
vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};
日期
2020 年 3 月 26 日 —— 江南烟雨
【LeetCode】999. Available Captures for Rook 解题报告(C++)的更多相关文章
- 【LeetCode】999. Available Captures for Rook 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- Leetcode 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...
- 【LEETCODE】46、999. Available Captures for Rook
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- 【ThermoRawFileParser】质谱raw格式转换mgf
众所周知,Proteowizard MSconvert用于质谱原始数据的格式转换,但主要平台是windows,要想在Linux上运行需要打Docker或Wine,对于普通用户来说还是很困难的,想想质谱 ...
- 【NCBI教程】资源汇总整理 (转载)
主题 网址 备注 [NCBI教程]资源汇总整理 http://www.omicshare.com/forum/thread-200-1-1.html (出处: OmicShare Forum)
- C++面试基础篇(二)
1.数组与指针的区别 数组下标运算实际上都是通过指针进行的. 数组名代表着指向该数组中下标为0的元素的指针,但有例外:sizeof(数组名)返回整个数组的大小,而非指针大小:&数组名返回一个指 ...
- 1小时学会Git玩转GitHub
版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 本次教程建议一边阅读一边用电脑实操 目录 一.了解Git和Github 1.1 什么是Git 1.2 什么是版本控制系统 1.3 什么是Github ...
- Spark基础:(五)Spark编程进阶
共享变量 (1)累加器:是用来对信息进行聚合的,同时也是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执 ...
- 零基础学习java------33---------http协议,tomcat(其如何在eclipse上发布),注册案例
一. HTTP协议 https://www.cnblogs.com/vamei/archive/2013/05/11/3069788.html 二. tomcat---------->web服务 ...
- jenkins的sonarqube之代码检测的两种方法
#:sonarqube下载地址,我们安装6.7 高版本已经不支持MySQL和Mariadb(最小3G内存) https://www.sonarqube.org/downloads/ #:安装文档 h ...
- ubantu安装maven
下载地址 http://maven.apache.org/download.cgi 或直接命令行下载 wget https://downloads.apache.org/maven/maven-3/3 ...
- 【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method
Python版 https://github.com/faif/python-patterns/blob/master/creational/factory_method.py #!/usr/bin/ ...
- MFC入门示例之树控件(CTreeControl)
1 //增加按钮 2 void CMFCApplication8Dlg::OnBnClickedButtonAdd() 3 { 4 //树中添加节点 5 CString strText; 6 GetD ...