Leetcode 999. 车的可用捕获量
- 用户通过次数255
- 用户尝试次数260
- 通过次数255
- 提交次数357
- 题目难度Easy
在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。
车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。
返回车能够在一次移动中捕获到的卒的数量。
示例 1:
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。
示例 2:
输入:[[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:0
解释:
象阻止了车捕获任何卒。
示例 3:
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
车可以捕获位置 b5,d6 和 f5 的卒。
提示:
board.length == board[i].length == 8
board[i][j]
可以是'R'
,'.'
,'B'
或'p'
- 只有一个格子上存在
board[i][j] == 'R'
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int n=,m=;
for(int i=;i < board.size();i++){
for(int j=;j < board[].size();j++){
if(board[i][j] == 'R'){n=i;m=j;}
}
}
int res = ;
for(int i=n-;i>=;i--){
if(board[i][m] == 'B')break;
else if(board[i][m] == 'p'){res++;break;}
}
for(int i=n+;i<board.size();i++){
if(board[i][m] == 'B')break;
else if(board[i][m] == 'p'){res++;break;}
}
for(int j=m-;j>=;j--){
if(board[n][j] == 'B')break;
else if(board[n][j] == 'p'){res++;break;}
}
for(int j=m+;j<board[].size();j++){
if(board[n][j] == 'B')break;
else if(board[n][j] == 'p'){res++;break;}
}
return res;
}
};
-HAOSHUIA
Leetcode 999. 车的可用捕获量的更多相关文章
- Java实现 LeetCode 999 车的可用捕获量(简单搜索)
999. 车的可用捕获量 在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 "R"," ...
- 【LeetCode】Available Captures for Rook(车的可用捕获量)
这道题是LeetCode里的第999道题. 题目叙述: 在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 &quo ...
- [Swift]LeetCode999. 车的可用捕获量 | Available Captures for Rook
在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 “R”,“.”,“B” 和 “p” 给出.大写字符表示白棋,小写 ...
- Leetcode 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Leetcode | 组目录
数组 [1]999. 车的可用捕获量 [2]989. 数组形式的整数加法
- leetcode 0217
目录 ✅ 682. 棒球比赛 描述 解答 cpp py ✅ 999. 车的可用捕获量 描述 解答 c other java todo py ✅ 118. 杨辉三角 描述 解答 cpp py ✅ 258 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- 2019年10~11月-NLP工程师求职记录
求职目标:NLP工程师 为什么想换工作? 除了技术相关书籍,我没读过太多其他类型的书,其中有一本内容短但是对我影响特别大的书--<谁动了我的奶酪>.出门问问是我毕业后的第一份工作,无论是工 ...
随机推荐
- 【Hadoop 分布式部署 十:配置HDFS 的HA、启动HA中的各个守护进程】
官方参考 配置 地址 :http://hadoop.apache.org/docs/r2.5.2/hadoop-project-dist/hadoop-hdfs/HDFSHighAvailabili ...
- kubernetes 实战5_命令_Assign Pods to Nodes&Configure a Pod to Use a ConfigMap
Assign Pods to Nodes how to assign a Kubernetes Pod to a particular node in a Kubernetes cluster. Ad ...
- 洛谷P1679神奇的四次方数--DP
原题请戳>>https://www.luogu.org/problem/show?pid=1679<< 题目描述 在你的帮助下,v神终于帮同学找到了最合适的大学,接下来就要通知 ...
- 02:httpd-2.2基础配置
---恢复内容开始--- 9.日志设定 错误日志: ErrorLog logs/error_log //这里使用了相对路径,相对于/etc/httpd/路径 LogLevel warn //定义日志 ...
- 史上最全Java面试题(带全部答案)
今天要谈的主题是关于求职,求职是在每个技术人员的生涯中都要经历多次.对于我们大部分人而言,在进入自己心仪的公司之前少不了准备工作,有一份全面细致面试题将帮助我们减少许多麻烦.在跳槽季来临之前,特地做这 ...
- oracle 12c创建可插拔数据库(PDB)与用户详解
前言 由于oracle 12c使用了CDB-PDB架构,类似于docker,在container-db内可以加载多个pluggable-db,因此安装后需要额外配置才能使用. 一.修改listener ...
- VC.判断双字节字符集前导字节集(IsDBCSLeadByte)
ZC:这是 WIndows API 函数 1.“BOOL IsDBCSLeadByte( char );” 判断 某字节是否在 双字节字符集的前导字节集中 ZC:可以判断 如 汉字.日文.韩文等 Z ...
- 剖析Hadoop和Spark的Shuffle过程差异
一.前言 对于基于MapReduce编程范式的分布式计算来说,本质上而言,就是在计算数据的交.并.差.聚合.排序等过程.而分布式计算分而治之的思想,让每个节点只计算部分数据,也就是只处理一个分片,那么 ...
- 01-python基础知识
1.这两个参数是什么意思:*args,**kwargs?我们为什么要使用它们? 答案 如果我们不确定要往函数中传入多少个参数,或者我们想往函数中以列表和元组的形式传参数时,那就使要用*args:如果我 ...
- 比对软件之STAR的使用方法
建索引 普通比对 二次比对 用于cufflinks和stringtie的比对 待续~ 参考:比对软件STAR的简单使用