|
| 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>给定一个 <code>8 x 8</code> 的棋盘,<strong>只有一个</strong> 白色的车,用字符 <code>'R'</code> 表示。棋盘上还可能存在白色的象 <code>'B'</code> 以及黑色的卒 <code>'p'</code>。空方块用字符 <code>'.'</code> 表示。</p> |
| 12 | + |
| 13 | +<p>车可以按水平或竖直方向(上,下,左,右)移动任意个方格直到它遇到另一个棋子或棋盘的边界。如果它能够在一次移动中移动到棋子的方格,则能够 <strong>吃掉</strong> 棋子。</p> |
| 14 | + |
| 15 | +<p>注意:车不能穿过其它棋子,比如象和卒。这意味着如果有其它棋子挡住了路径,车就不能够吃掉棋子。</p> |
| 16 | + |
| 17 | +<p>返回白车将能 <strong>吃掉</strong> 的 <strong>卒的数量</strong>。</p> |
| 18 | + |
| 19 | +<p> </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> </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> 可以是 <code>'R'</code>,<code>'.'</code>,<code>'B'</code> 或 <code>'p'</code></li> |
| 62 | + <li>只有一个格子上存在 <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) |
0 commit comments