Skip to content

Commit cd0c22d

Browse files
authored
Merge pull request #643 from LetMeFly666/999
添加问题“999.可以被一步捕获的棋子数”的代码和题解
2 parents d4a62f0 + 7d598c7 commit cd0c22d

File tree

6 files changed

+295
-1
lines changed

6 files changed

+295
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-07 00:02:55
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-07 00:18:47
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
#ifdef FirstTry // WA,只进行了一次break
12+
const int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
13+
class Solution {
14+
public:
15+
int numRookCaptures(vector<vector<char>>& board) {
16+
int x, y;
17+
for (x = 0; x < board.size(); x++) {
18+
for (y = 0; y < board[0].size(); y++) {
19+
if (board[x][y] == 'R') {
20+
break;
21+
}
22+
}
23+
}
24+
int ans = 0;
25+
for (int d = 0; d < 4; d++) {
26+
for (int step = 1; ; step++) {
27+
int nx = x + directions[d][0] * step;
28+
int ny = y + directions[d][1] * step;
29+
if (nx < 0 || nx >= board.size() || ny < 0 || ny >= board[0].size() || board[nx][ny] == 'B') {
30+
break;
31+
}
32+
ans += board[nx][ny] == 'p';
33+
}
34+
}
35+
return ans;
36+
}
37+
};
38+
#else // FirstTry
39+
// SecondTry
40+
const int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
41+
class Solution {
42+
public:
43+
int numRookCaptures(vector<vector<char>>& board) {
44+
int x, y;
45+
for (x = 0; x < board.size(); x++) {
46+
for (y = 0; y < board[0].size(); y++) {
47+
if (board[x][y] == 'R') {
48+
goto loop;
49+
}
50+
}
51+
}
52+
loop:;
53+
int ans = 0;
54+
for (int d = 0; d < 4; d++) {
55+
for (int step = 1; ; step++) {
56+
int nx = x + directions[d][0] * step;
57+
int ny = y + directions[d][1] * step;
58+
if (nx < 0 || nx >= board.size() || ny < 0 || ny >= board[0].size() || board[nx][ny] == 'B') {
59+
break;
60+
}
61+
if (board[nx][ny] == 'p') {
62+
ans++;
63+
break;
64+
}
65+
}
66+
}
67+
return ans;
68+
}
69+
};
70+
#endif // FirstTry
71+
72+
#ifdef _WIN32
73+
// [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
74+
int main() {
75+
Solution sol;
76+
string s;
77+
while (cin >> s) {
78+
vector<vector<char>> v = stringToVectorVectorC(s);
79+
cout << sol.numRookCaptures(v) << endl;
80+
}
81+
return 0;
82+
}
83+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
|0993.二叉树的堂兄弟节点|简单|<a href="https://leetcode.cn/problems/cousins-in-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/08/LeetCode%200993.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%A0%82%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136078040" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/cousins-in-binary-tree/solutions/2635757/letmefly-993er-cha-shu-de-tang-xiong-di-eyvq6/" target="_blank">LeetCode题解</a>|
321321
|0994.腐烂的橘子|中等|<a href="https://leetcode.cn/problems/rotting-oranges/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/13/LeetCode%200994.%E8%85%90%E7%83%82%E7%9A%84%E6%A9%98%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138802167" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rotting-oranges/solutions/2776028/letmefly-994fu-lan-de-ju-zi-yan-du-you-x-45fu/" target="_blank">LeetCode题解</a>|
322322
|0997.找到小镇的法官|简单|<a href="https://leetcode.cn/problems/find-the-town-judge/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/22/LeetCode%200997.%E6%89%BE%E5%88%B0%E5%B0%8F%E9%95%87%E7%9A%84%E6%B3%95%E5%AE%98/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142446520" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-town-judge/solutions/2926743/letmefly-997zhao-dao-xiao-zhen-de-fa-gua-9e5w/" target="_blank">LeetCode题解</a>|
323+
|0999.可以被一步捕获的棋子数|简单|<a href="https://leetcode.cn/problems/available-captures-for-rook/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/07/LeetCode%200999.%E5%8F%AF%E4%BB%A5%E8%A2%AB%E4%B8%80%E6%AD%A5%E6%8D%95%E8%8E%B7%E7%9A%84%E6%A3%8B%E5%AD%90%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144303138" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/available-captures-for-rook/solutions/3012619/letmefly-999ke-yi-bei-yi-bu-bu-huo-de-qi-z7fw/" target="_blank">LeetCode题解</a>|
323324
|1003.检查替换后的词是否有效|中等|<a href="https://leetcode.cn/problems/check-if-word-is-valid-after-substitutions/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/03/LeetCode%201003.%E6%A3%80%E6%9F%A5%E6%9B%BF%E6%8D%A2%E5%90%8E%E7%9A%84%E8%AF%8D%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130470201" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-word-is-valid-after-substitutions/solutions/2254002/letmefly-1003jian-cha-ti-huan-hou-de-ci-we7vj/" target="_blank">LeetCode题解</a>|
324325
|1010.总持续时间可被60整除的歌曲|中等|<a href="https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/07/LeetCode%201010.%E6%80%BB%E6%8C%81%E7%BB%AD%E6%97%B6%E9%97%B4%E5%8F%AF%E8%A2%AB60%E6%95%B4%E9%99%A4%E7%9A%84%E6%AD%8C%E6%9B%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130544996" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/solutions/2260413/letmefly-1010zong-chi-xu-shi-jian-ke-bei-c8di/" target="_blank">LeetCode题解</a>|
325326
|1021.删除最外层的括号|简单|<a href="https://leetcode.cn/problems/remove-outermost-parentheses/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/05/28/LeetCode%201021.%E5%88%A0%E9%99%A4%E6%9C%80%E5%A4%96%E5%B1%82%E7%9A%84%E6%8B%AC%E5%8F%B7" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125015777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-outermost-parentheses/solution/letmefly-1021shan-chu-zui-wai-ceng-de-gu-nyio/" target="_blank">LeetCode题解</a>|
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: 999.可以被一步捕获的棋子数
3+
date: 2024-12-07 00:25:18
4+
tags: [题解, LeetCode, 简单, 数组, 矩阵, 模拟]
5+
---
6+
7+
# 【LetMeFly】999.可以被一步捕获的棋子数:模拟
8+
9+
力扣题目链接:[https://leetcode.cn/problems/available-captures-for-rook/](https://leetcode.cn/problems/available-captures-for-rook/)
10+
11+
<p>给定一个&nbsp;<code>8 x 8</code> 的棋盘,<strong>只有一个</strong> 白色的车,用字符 <code>'R'</code> 表示。棋盘上还可能存在白色的象&nbsp;<code>'B'</code>&nbsp;以及黑色的卒&nbsp;<code>'p'</code>。空方块用字符 <code>'.'</code>&nbsp;表示。</p>
12+
13+
<p>车可以按水平或竖直方向(上,下,左,右)移动任意个方格直到它遇到另一个棋子或棋盘的边界。如果它能够在一次移动中移动到棋子的方格,则能够 <strong>吃掉</strong> 棋子。</p>
14+
15+
<p>注意:车不能穿过其它棋子,比如象和卒。这意味着如果有其它棋子挡住了路径,车就不能够吃掉棋子。</p>
16+
17+
<p>返回白车将能 <strong>吃掉</strong> 的 <strong>卒的数量</strong>。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_1_improved.PNG" style="height: 305px; width: 300px;" /></p>
24+
25+
<pre>
26+
<strong>输入:</strong>[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
27+
<strong>输出:</strong>3
28+
<strong>解释:
29+
</strong>在本例中,车能够吃掉所有的卒。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_2_improved.PNG" style="height: 306px; width: 300px;" /></p>
35+
36+
<pre>
37+
<strong>输入:</strong>[[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
38+
<strong>输出:</strong>0
39+
<strong>解释:
40+
</strong>象阻止了车吃掉任何卒。
41+
</pre>
42+
43+
<p><strong>示例 3:</strong></p>
44+
45+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_3_improved.PNG" style="height: 305px; width: 300px;" /></p>
46+
47+
<pre>
48+
<strong>输入:</strong>[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
49+
<strong>输出:</strong>3
50+
<strong>解释: </strong>
51+
车可以吃掉位置 b5,d6 和 f5 的卒。
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ol>
59+
<li><code>board.length == 8</code></li>
60+
<li><code>board[i].length == 8</code></li>
61+
<li><code>board[i][j]</code> 可以是&nbsp;<code>'R'</code>,<code>'.'</code>,<code>'B'</code>&nbsp;或&nbsp;<code>'p'</code></li>
62+
<li>只有一个格子上存在&nbsp;<code>board[i][j] == 'R'</code></li>
63+
</ol>
64+
65+
66+
67+
## 解题方法:模拟
68+
69+
一共分为两步:
70+
71+
1. 遍历棋盘,遇到字符`R`时停下,并记录下起点下标
72+
2. 从起点开始分别向上下左右四个方向遍历,遇到边界或者遇到`B`停止。同时,遍历时若遇到`p`,则答案数量加一并停止。
73+
74+
+ 时间复杂度$O(mn)$,其中棋盘大小为$m\times n$
75+
+ 空间复杂度$O(1)$
76+
77+
[我的一个评论](https://leetcode.cn/problems/available-captures-for-rook/solutions/170099/che-de-ke-yong-bu-huo-liang-by-leetcode-solution/comments/2438607)
78+
79+
### AC代码
80+
81+
#### C++
82+
83+
```cpp
84+
const int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
85+
class Solution {
86+
public:
87+
int numRookCaptures(vector<vector<char>>& board) {
88+
int x, y;
89+
for (x = 0; x < board.size(); x++) {
90+
for (y = 0; y < board[0].size(); y++) {
91+
if (board[x][y] == 'R') {
92+
goto loop;
93+
}
94+
}
95+
}
96+
loop:;
97+
int ans = 0;
98+
for (int d = 0; d < 4; d++) {
99+
for (int step = 1; ; step++) {
100+
int nx = x + directions[d][0] * step;
101+
int ny = y + directions[d][1] * step;
102+
if (nx < 0 || nx >= board.size() || ny < 0 || ny >= board[0].size() || board[nx][ny] == 'B') {
103+
break;
104+
}
105+
if (board[nx][ny] == 'p') {
106+
ans++;
107+
break;
108+
}
109+
}
110+
}
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/12/07/LeetCode%200999.%E5%8F%AF%E4%BB%A5%E8%A2%AB%E4%B8%80%E6%AD%A5%E6%8D%95%E8%8E%B7%E7%9A%84%E6%A3%8B%E5%AD%90%E6%95%B0/)哦~
117+
>
118+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144303138](https://letmefly.blog.csdn.net/article/details/144303138)

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ xx型
167167
|します||
168168
|やって||
169169
|やり||
170+
|切り(きり)||
170171
|磨き(みがき)||
171172
|使い(つかい)|使用|
172173
|乗り(のり)|乘坐|
@@ -317,6 +318,7 @@ xx型
317318
|寒い(さむい)|冷的|
318319
|狭い(せまい)|狭小的|
319320
|広い(ひろい)|(学校等)大的|
321+
|まるい|圆的|
320322
|危ない(あぶない)|危险的|
321323
|同じ(おなじ)|一样的|
322324
|ゆっくり|慢慢|
@@ -333,6 +335,8 @@ xx型
333335
|かぜ||
334336
|いす|椅子|
335337
|手紙(てがみ)||
338+
|紙(かみ)||
339+
|カギ|钥匙|
336340
|本(ほん)||
337341
|ペン||
338342
|本棚(ほんだな)|书架|
@@ -1080,7 +1084,10 @@ xx型
10801084
|その<ruby>後<rt>go</rt></ruby>、映画館に行きました。<br/>那之后去了电影院。|
10811085
|映画のチケットは<ruby>後<rt>ato</rt></ruby>。<br/>电影票之后再买。|
10821086
|行き<font color="#ce82ff">たくない</font>です。<br/>不想去。<details><summary>(自己)想做某事</summary>想要对方做某事:动词<font color="#ce82ff">て</font>形加上<font color="#ce82ff">ほしい</font><br/>自己想要做某事:动词<font color="#ce82ff">ます</font>形加上<font color="#ce82ff">たい</font><br/>自己****想做某事:把<font color="#ce82ff">たい</font>换成<font color="#ce82ff">たくない</font></details>|
1083-
|友たちと映画を見たり、公園へ行ったりします。<br/>和朋友看看电影,逛逛公园|
1087+
|友たちと映画を見たり、公園へ行ったりします。<br/>和朋友看看电影,逛逛公园。|
1088+
|紙を2<font color="#28bea0">枚</font>ください。<br/>请给我两张纸。|
1089+
|そのお皿はまるいです。<br/>那个盘子是圆的。|
1090+
|パンをナイフで切ってください。<br/>请用刀切面包。|
10841091

10851092
[单词添加时间](https://github.com/LetMeFly666/LeetCode/blame/master/Solutions/Other-Japanese-LearningNotes.md)
10861093

tryGoPy/temp-20241206-回国后.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-06 21:08:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-06 21:08:35
6+
-->
7+
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git pull origin master
8+
remote: Enumerating objects: 27, done.
9+
remote: Counting objects: 100% (27/27), done.
10+
remote: Compressing objects: 100% (14/14), done.
11+
remote: Total 27 (delta 17), reused 21 (delta 13), pack-reused 0 (from 0)
12+
Unpacking objects: 100% (27/27), 10.49 KiB | 5.00 KiB/s, done.
13+
From ssh.github.com:LetMeFly666/LeetCode
14+
* branch master -> FETCH_HEAD
15+
37a4490d3d6..d4a62f0e38f master -> origin/master
16+
Updating 37a4490d3d6..d4a62f0e38f
17+
Fast-forward
18+
Codes/0743-network-delay-time_20241125-half.cpp | 27 +++
19+
Codes/3206-alternating-groups-i.cpp | 22 +++
20+
Codes/3206-alternating-groups-i.go | 16 ++
21+
Codes/3206-alternating-groups-i.java | 17 ++
22+
Codes/3206-alternating-groups-i.py | 11 ++
23+
Codes/3208-alternating-groups-ii.cpp | 28 +++
24+
Codes/3208-alternating-groups-ii.go | 28 +++
25+
Codes/3208-alternating-groups-ii.java | 29 ++++
26+
Codes/3208-alternating-groups-ii.py | 17 ++
27+
README.md | 2 +
28+
... 3206.\344\272\244\346\233\277\347\273\204I.md" | 136 +++++++++++++++
29+
...3208.\344\272\244\346\233\277\347\273\204II.md" | 191 +++++++++++++++++++++
30+
Solutions/Other-English-LearningNotes-SomeWords.md | 13 ++
31+
tryGoPy/CHANGELOG.md | 111 ++++++++++++
32+
14 files changed, 648 insertions(+)
33+
create mode 100644 Codes/0743-network-delay-time_20241125-half.cpp
34+
create mode 100644 Codes/3206-alternating-groups-i.cpp
35+
create mode 100644 Codes/3206-alternating-groups-i.go
36+
create mode 100644 Codes/3206-alternating-groups-i.java
37+
create mode 100644 Codes/3206-alternating-groups-i.py
38+
create mode 100644 Codes/3208-alternating-groups-ii.cpp
39+
create mode 100644 Codes/3208-alternating-groups-ii.go
40+
create mode 100644 Codes/3208-alternating-groups-ii.java
41+
create mode 100644 Codes/3208-alternating-groups-ii.py
42+
create mode 100644 "Solutions/LeetCode 3206.\344\272\244\346\233\277\347\273\204I.md"
43+
create mode 100644 "Solutions/LeetCode 3208.\344\272\244\346\233\277\347\273\204II.md"
44+
create mode 100644 tryGoPy/CHANGELOG.md
45+
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git log
46+
commit d4a62f0e38f370fc5dda840981a9ef131796fbe3 (HEAD -> master, origin/master)
47+
Merge: bce53fccbaf 9c352c989cf
48+
Author: LetMeFly <[email protected]>
49+
Date: Thu Nov 28 23:50:30 2024 +0800
50+
51+
Merge pull request #641 from LetMeFly666/3208
52+
53+
添加问题“3208.交替组II”的代码和题解
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
F:\OtherApps\Program\Git\Store\Store2_Web_Various\various>git pull
2+
remote: Enumerating objects: 23, done.
3+
remote: Counting objects: 100% (23/23), done.
4+
remote: Compressing objects: 100% (2/2), done.
5+
remote: Total 15 (delta 10), reused 15 (delta 10), pack-reused 0 (from 0)
6+
Unpacking objects: 100% (15/15), 1.61 KiB | 2.00 KiB/s, done.
7+
From github.com:LetMeFly666/various
8+
865c2ac..e9f950b dev -> original/dev
9+
865c2ac..e9f950b master -> original/master
10+
warning: It took 10.50 seconds to check forced updates. You can use
11+
'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'
12+
to avoid this check.
13+
14+
error: Your local changes to the following files would be overwritten by merge:
15+
ACM/LeetCode/index.html
16+
Please commit your changes or stash them before you merge.
17+
Aborting
18+
Updating 865c2ac..e9f950b
19+
20+
F:\OtherApps\Program\Git\Store\Store2_Web_Various\various>git status
21+
On branch dev
22+
Your branch is behind 'original/dev' by 3 commits, and can be fast-forwarded.
23+
(use "git pull" to update your local branch)
24+
25+
nothing to commit, working tree clean
26+
F:\OtherApps\Program\Git\Store\Store2_Web_Various\various>git pull
27+
Updating 865c2ac..e9f950b
28+
Fast-forward
29+
ACM/LeetCode/index.html | 8 ++++++--
30+
1 file changed, 6 insertions(+), 2 deletions(-)
31+
32+
F:\OtherApps\Program\Git\Store\Store2_Web_Various\various>

0 commit comments

Comments
 (0)