题目地址: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:

  1. board.length == board[i].length == 8
  2. board[i][j] is either 'R', '.', 'B', or 'p'
  3. There is exactly one cell with board[i][j] == 'R'

题目大意

国际象棋的棋盘上有一个白方的车,还有若干个白方的象,还有若干黑方的卒。车可以向四个方向行走,白色的象会挡住车的去路,问最多能吃掉多少个黑色的卒。

解题方法

四方向搜索

这个题和炸弹人的题目是一样的。

  1. 先找到白色的车的位置;
  2. 然后从该位置出发,向四个方向进行搜索,如果遇到边界或者白色的象那么停止搜索;遇到黑色的卒那么就把能吃的卒的数目加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++)的更多相关文章

  1. 【LeetCode】999. Available Captures for Rook 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  2. Leetcode 999. Available Captures for Rook

    class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...

  3. 【LEETCODE】46、999. Available Captures for Rook

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  7. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. MISA(在线)注释叶绿体基因组SSR

    SSR (Simple Sequence Repeat),即简单重复序列,是一种以PCR技术为核心的DNA分子标记技术,也称为微卫星序列或者串联重复. 简单重复顾名思义就是以很短的序列为一个单元,比如 ...

  2. Synteny和collinear的区别

    在比较基因组学的时候,经常会听到"共线性"这个词,但是与其对应的有两个不同的概念,即 (1) synteny (2) collinear 二者的区别如下图所示: 可以看到,synt ...

  3. 【Python小试】将核酸序列翻译成氨基酸序列

    三联密码表 gencode = { 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M', 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT' ...

  4. nohup使用

    nohup:不挂断运行 在忽略挂起信号的情况下运行给定的命令,以便在注销后命令可以在后台继续运行. 可以这么理解:不挂断的运行,注意并没有后台运行的功能,就是指,用nohup 运行命令可以是命令永远运 ...

  5. zabbix 集成cloud alert

    1.       了解 Cloud Alert 通过应用,接入监控系统/平台的告警,集中管理您的告警,统一分派通知,统一分析.这个平台最先了解和使用是在 2017 年下半年,之前的名称叫 oneits ...

  6. 压力测试工具——apchebench(简称ab)

    ab的原理 ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx.li ...

  7. ubuntu终端ls颜色配置

    buntu中没有LS_COLORS,/etc/目录中也没有DIR_COLORS,所以这里使用dircolor命令加以解决 1. 利用dircolors命令,查看我们的系统当前的文件名称显示颜色的值,然 ...

  8. Prometheus_exporter安装与使用

    Promethues概述:可以看一下更详细的介绍,以下为转载的博客,原文链接,支持原创,请多多支持!!:闫世成的博客园 Prometheus-node-exporter 1.简介: 内核公开的硬件和操 ...

  9. HDFS【hadoop3.1.3 windows开发环境搭建】

    目录 一.配置hadoop3.1.3 windows环境依赖 配置环境变量 添加到path路径 在cmd中测试 二.idea中的配置 创建工程/模块 添加pom.xml依赖 日志添加--配置log4j ...

  10. nodeJs,Express中间件是什么与常见中间件

    中间件的功能和分类 中间件的本质就是一个函数,在收到请求和返回相应的过程中做一些我们想做的事情.Express文档中对它的作用是这么描述的: 执行任何代码.修改请求和响应对象.终结请求-响应循环.调用 ...