Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6cc524d

Browse files
feat: add solutions to lc problem: No.0999 (doocs#3841)
1 parent 70ce676 commit 6cc524d

File tree

8 files changed

+352
-160
lines changed

8 files changed

+352
-160
lines changed

‎solution/0900-0999/0999.Available Captures for Rook/README.md‎

Lines changed: 120 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ tags:
8080

8181
### 方法一:模拟
8282

83-
我们先遍历棋盘,找到车的位置 $(x, y),ドル然后从 $(x, y)$ 出发,向上下左右四个方向遍历:
83+
我们先遍历棋盘,找到车的位置 $(i, j),ドル然后从 $(i, j)$ 出发,向上下左右四个方向遍历:
8484

85-
- 如果遇到象或者边界,那么该方向停止遍历;
86-
- 如果遇到卒,那么答案加一,然后该方向停止遍历;
87-
- 否则,继续遍历。
85+
- 如果不是边界且不是象,则继续向前走;
86+
- 如果是卒,则答案加一,并停止该方向的遍历。
8887

8988
遍历完四个方向后,即可得到答案。
9089

@@ -97,50 +96,49 @@ tags:
9796
```python
9897
class Solution:
9998
def numRookCaptures(self, board: List[List[str]]) -> int:
100-
ans = 0
10199
dirs = (-1, 0, 1, 0, -1)
102-
for i in range(8):
103-
for j in range(8):
100+
n = len(board)
101+
for i in range(n):
102+
for j in range(n):
104103
if board[i][j] == "R":
104+
ans = 0
105105
for a, b in pairwise(dirs):
106-
x, y = i, j
107-
while 0 <= x + a < 8 and 0 <= y + b < 8:
108-
x, y = x + a, y + b
106+
x, y = i + a, j + b
107+
while 0 <= x < n and 0 <= y < n and board[x][y] != "B":
109108
if board[x][y] == "p":
110109
ans += 1
111110
break
112-
if board[x][y] == "B":
113-
break
114-
return ans
111+
x, y = x + a, y + b
112+
return ans
115113
```
116114

117115
#### Java
118116

119117
```java
120118
class Solution {
121119
public int numRookCaptures(char[][] board) {
122-
int ans = 0;
123-
int[] dirs = {-1, 0, 1, 0, -1};
124-
for (int i = 0; i < 8; ++i) {
125-
for (int j = 0; j < 8; ++j) {
120+
finalint[] dirs = {-1, 0, 1, 0, -1};
121+
int n = board.length;
122+
for (int i = 0; i < n; ++i) {
123+
for (int j = 0; j < n; ++j) {
126124
if (board[i][j] == 'R') {
125+
int ans = 0;
127126
for (int k = 0; k < 4; ++k) {
128-
int x = i, y = j;
129-
int a = dirs[k], b = dirs[k + 1];
130-
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8
131-
&& board[x + a][y + b] != 'B') {
132-
x += a;
133-
y += b;
127+
int x = i + dirs[k], y = j + dirs[k + 1];
128+
while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') {
134129
if (board[x][y] == 'p') {
135130
++ans;
136131
break;
137132
}
133+
x += dirs[k];
134+
y += dirs[k + 1];
138135
}
139136
}
137+
return ans;
140138
}
141139
}
142140
}
143-
return ans;
141+
return 0;
144142
}
145143
}
146144
```
@@ -151,27 +149,28 @@ class Solution {
151149
class Solution {
152150
public:
153151
int numRookCaptures(vector<vector<char>>& board) {
154-
int ans = 0;
155-
int dirs[5] = {-1, 0, 1, 0, -1};
156-
for (int i = 0; i < 8; ++i) {
157-
for (int j = 0; j < 8; ++j) {
152+
const int dirs[5] = {-1, 0, 1, 0, -1};
153+
int n = board.size();
154+
for (int i = 0; i < n; ++i) {
155+
for (int j = 0; j < n; ++j) {
158156
if (board[i][j] == 'R') {
157+
int ans = 0;
159158
for (int k = 0; k < 4; ++k) {
160-
int x = i, y = j;
161-
int a = dirs[k], b = dirs[k + 1];
162-
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
163-
x += a;
164-
y += b;
159+
int x = i + dirs[k], y = j + dirs[k + 1];
160+
while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') {
165161
if (board[x][y] == 'p') {
166162
++ans;
167163
break;
168164
}
165+
x += dirs[k];
166+
y += dirs[k + 1];
169167
}
170168
}
169+
return ans;
171170
}
172171
}
173172
}
174-
return ans;
173+
return 0;
175174
}
176175
};
177176
```
@@ -180,25 +179,93 @@ public:
180179
181180
```go
182181
func numRookCaptures(board [][]byte) (ans int) {
183-
dirs := [5]int{-1, 0, 1, 0, -1}
184-
for i := 0; i < 8; i++ {
185-
for j := 0; j < 8; j++ {
186-
if board[i][j] == 'R' {
187-
for k := 0; k < 4; k++ {
188-
x, y := i, j
189-
a, b := dirs[k], dirs[k+1]
190-
for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' {
191-
x, y = x+a, y+b
192-
if board[x][y] == 'p' {
193-
ans++
194-
break
195-
}
196-
}
197-
}
198-
}
199-
}
200-
}
201-
return
182+
dirs := []int{-1, 0, 1, 0, -1}
183+
n := len(board)
184+
for i := 0; i < n; i++ {
185+
for j := 0; j < n; j++ {
186+
if board[i][j] == 'R' {
187+
for k := 0; k < 4; k++ {
188+
x, y := i + dirs[k], j + dirs[k+1]
189+
for x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B' {
190+
if board[x][y] == 'p' {
191+
ans++
192+
break
193+
}
194+
x += dirs[k]
195+
y += dirs[k+1]
196+
}
197+
}
198+
return
199+
}
200+
}
201+
}
202+
return
203+
}
204+
```
205+
206+
#### TypeScript
207+
208+
```ts
209+
function numRookCaptures(board: string[][]): number {
210+
const dirs = [-1, 0, 1, 0, -1];
211+
const n = board.length;
212+
for (let i = 0; i < n; i++) {
213+
for (let j = 0; j < n; j++) {
214+
if (board[i][j] === 'R') {
215+
let ans = 0;
216+
for (let k = 0; k < 4; k++) {
217+
let [x, y] = [i + dirs[k], j + dirs[k + 1]];
218+
while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] !== 'B') {
219+
if (board[x][y] === 'p') {
220+
ans++;
221+
break;
222+
}
223+
x += dirs[k];
224+
y += dirs[k + 1];
225+
}
226+
}
227+
return ans;
228+
}
229+
}
230+
}
231+
return 0;
232+
}
233+
```
234+
235+
#### Rust
236+
237+
```rust
238+
impl Solution {
239+
pub fn num_rook_captures(board: Vec<Vec<char>>) -> i32 {
240+
let dirs = [-1, 0, 1, 0, -1];
241+
let n = board.len();
242+
for i in 0..n {
243+
for j in 0..n {
244+
if board[i][j] == 'R' {
245+
let mut ans = 0;
246+
for k in 0..4 {
247+
let mut x = i as i32 + dirs[k];
248+
let mut y = j as i32 + dirs[k + 1];
249+
while x >= 0
250+
&& x < n as i32
251+
&& y >= 0
252+
&& y < n as i32
253+
&& board[x as usize][y as usize] != 'B'
254+
{
255+
if board[x as usize][y as usize] == 'p' {
256+
ans += 1;
257+
break;
258+
}
259+
x += dirs[k];
260+
y += dirs[k + 1];
261+
}
262+
}
263+
return ans;
264+
}
265+
}
266+
}
267+
0
268+
}
202269
}
203270
```
204271

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /