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 56ddfc6

Browse files
committed
feat: update solutions to lc problem: No.0999
No.0999.Available Captures for Rook
1 parent cd6b160 commit 56ddfc6

File tree

6 files changed

+200
-256
lines changed

6 files changed

+200
-256
lines changed

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

Lines changed: 74 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68-
先找到 R 的位置,之后向"上、下、左、右"四个方向查找,累加结果。
68+
**方法一:模拟**
69+
70+
我们先遍历棋盘,找到车的位置 $(x, y),ドル然后从 $(x, y)$ 出发,向上下左右四个方向遍历:
71+
72+
- 如果遇到象或者边界,那么该方向停止遍历;
73+
- 如果遇到卒,那么答案加一,然后该方向停止遍历;
74+
- 否则,继续遍历。
75+
76+
遍历完四个方向后,即可得到答案。
77+
78+
时间复杂度 $O(m \times n),ドル其中 $m$ 和 $n$ 分别是棋盘的行数和列数,本题中 $m = n = 8$。空间复杂度 $O(1)$。
6979

7080
<!-- tabs:start -->
7181

@@ -76,20 +86,20 @@
7686
```python
7787
class Solution:
7888
def numRookCaptures(self, board: List[List[str]]) -> int:
79-
x, y, n = 0, 0, 8
80-
for i in range(n):
81-
for j in range(n):
82-
if board[i][j] == 'R':
83-
x, y = i, j
84-
break
8589
ans = 0
86-
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
87-
i, j = x, y
88-
while 0 <= i + a < n and 0 <= j + b < n and board[i + a][j + b] != 'B':
89-
i, j = i + a, j + b
90-
if board[i][j] == 'p':
91-
ans += 1
92-
break
90+
dirs = (-1, 0, 1, 0, -1)
91+
for i in range(8):
92+
for j in range(8):
93+
if board[i][j] == "R":
94+
for a, b in pairwise(dirs):
95+
x, y = i, j
96+
while 0 <= x + a < 8 and 0 <= y + b < 8:
97+
x, y = x + a, y + b
98+
if board[x][y] == "p":
99+
ans += 1
100+
break
101+
if board[x][y] == "B":
102+
break
93103
return ans
94104
```
95105

@@ -100,34 +110,27 @@ class Solution:
100110
```java
101111
class Solution {
102112
public int numRookCaptures(char[][] board) {
103-
int[] pos = find(board);
104-
int ans = 0, n = 8;
105-
int[][] dirs = new int[][] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
106-
for (int[] dir : dirs) {
107-
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
108-
while (
109-
x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
110-
x += a;
111-
y += b;
112-
if (board[x][y] == 'p') {
113-
++ans;
114-
break;
115-
}
116-
}
117-
}
118-
return ans;
119-
}
120-
121-
private int[] find(char[][] board) {
122-
int n = 8;
123-
for (int i = 0; i < n; ++i) {
124-
for (int j = 0; j < n; ++j) {
113+
int ans = 0;
114+
int[] dirs = {-1, 0, 1, 0, -1};
115+
for (int i = 0; i < 8; ++i) {
116+
for (int j = 0; j < 8; ++j) {
125117
if (board[i][j] == 'R') {
126-
return new int[] {i, j};
118+
for (int k = 0; k < 4; ++k) {
119+
int x = i, y = j;
120+
int a = dirs[k], b = dirs[k + 1];
121+
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
122+
x += a;
123+
y += b;
124+
if (board[x][y] == 'p') {
125+
++ans;
126+
break;
127+
}
128+
}
129+
}
127130
}
128131
}
129132
}
130-
return null;
133+
return ans;
131134
}
132135
}
133136
```
@@ -138,69 +141,54 @@ class Solution {
138141
class Solution {
139142
public:
140143
int numRookCaptures(vector<vector<char>>& board) {
141-
vector<int> pos = find(board);
142-
int ans = 0, n = 8;
143-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
144-
for (auto& dir : dirs) {
145-
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
146-
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
147-
x += a;
148-
y += b;
149-
if (board[x][y] == 'p') {
150-
++ans;
151-
break;
152-
}
153-
}
154-
}
155-
return ans;
156-
}
157-
158-
vector<int> find(vector<vector<char>>& board) {
159-
int n = 8;
160-
for (int i = 0; i < n; ++i) {
161-
for (int j = 0; j < n; ++j) {
144+
int ans = 0;
145+
int dirs[5] = {-1, 0, 1, 0, -1};
146+
for (int i = 0; i < 8; ++i) {
147+
for (int j = 0; j < 8; ++j) {
162148
if (board[i][j] == 'R') {
163-
return {i, j};
149+
for (int k = 0; k < 4; ++k) {
150+
int x = i, y = j;
151+
int a = dirs[k], b = dirs[k + 1];
152+
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
153+
x += a;
154+
y += b;
155+
if (board[x][y] == 'p') {
156+
++ans;
157+
break;
158+
}
159+
}
160+
}
164161
}
165162
}
166163
}
167-
return {};
164+
return ans;
168165
}
169166
};
170167
```
171168
172169
### **Go**
173170
174171
```go
175-
func numRookCaptures(board [][]byte) int {
176-
n := 8
177-
178-
find := func() []int {
179-
for i := 0; i < n; i++ {
180-
for j := 0; j < n; j++ {
181-
if board[i][j] == 'R' {
182-
return []int{i, j}
172+
func numRookCaptures(board [][]byte) (ans int) {
173+
dirs := [5]int{-1, 0, 1, 0, -1}
174+
for i := 0; i < 8; i++ {
175+
for j := 0; j < 8; j++ {
176+
if board[i][j] == 'R' {
177+
for k := 0; k < 4; k++ {
178+
x, y := i, j
179+
a, b := dirs[k], dirs[k+1]
180+
for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' {
181+
x, y = x+a, y+b
182+
if board[x][y] == 'p' {
183+
ans++
184+
break
185+
}
186+
}
183187
}
184188
}
185189
}
186-
return []int{}
187-
}
188-
189-
pos := find()
190-
ans := 0
191-
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
192-
for _, dir := range dirs {
193-
x, y, a, b := pos[0], pos[1], dir[0], dir[1]
194-
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != 'B' {
195-
x += a
196-
y += b
197-
if board[x][y] == 'p' {
198-
ans++
199-
break
200-
}
201-
}
202190
}
203-
return ans
191+
return
204192
}
205193
```
206194

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

Lines changed: 63 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@
5454
```python
5555
class Solution:
5656
def numRookCaptures(self, board: List[List[str]]) -> int:
57-
x, y, n = 0, 0, 8
58-
for i in range(n):
59-
for j in range(n):
60-
if board[i][j] == 'R':
61-
x, y = i, j
62-
break
6357
ans = 0
64-
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
65-
i, j = x, y
66-
while 0 <= i + a < n and 0 <= j + b < n and board[i + a][j + b] != 'B':
67-
i, j = i + a, j + b
68-
if board[i][j] == 'p':
69-
ans += 1
70-
break
58+
dirs = (-1, 0, 1, 0, -1)
59+
for i in range(8):
60+
for j in range(8):
61+
if board[i][j] == "R":
62+
for a, b in pairwise(dirs):
63+
x, y = i, j
64+
while 0 <= x + a < 8 and 0 <= y + b < 8:
65+
x, y = x + a, y + b
66+
if board[x][y] == "p":
67+
ans += 1
68+
break
69+
if board[x][y] == "B":
70+
break
7171
return ans
7272
```
7373

@@ -76,34 +76,27 @@ class Solution:
7676
```java
7777
class Solution {
7878
public int numRookCaptures(char[][] board) {
79-
int[] pos = find(board);
80-
int ans = 0, n = 8;
81-
int[][] dirs = new int[][] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
82-
for (int[] dir : dirs) {
83-
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
84-
while (
85-
x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
86-
x += a;
87-
y += b;
88-
if (board[x][y] == 'p') {
89-
++ans;
90-
break;
91-
}
92-
}
93-
}
94-
return ans;
95-
}
96-
97-
private int[] find(char[][] board) {
98-
int n = 8;
99-
for (int i = 0; i < n; ++i) {
100-
for (int j = 0; j < n; ++j) {
79+
int ans = 0;
80+
int[] dirs = {-1, 0, 1, 0, -1};
81+
for (int i = 0; i < 8; ++i) {
82+
for (int j = 0; j < 8; ++j) {
10183
if (board[i][j] == 'R') {
102-
return new int[] {i, j};
84+
for (int k = 0; k < 4; ++k) {
85+
int x = i, y = j;
86+
int a = dirs[k], b = dirs[k + 1];
87+
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
88+
x += a;
89+
y += b;
90+
if (board[x][y] == 'p') {
91+
++ans;
92+
break;
93+
}
94+
}
95+
}
10396
}
10497
}
10598
}
106-
return null;
99+
return ans;
107100
}
108101
}
109102
```
@@ -114,69 +107,54 @@ class Solution {
114107
class Solution {
115108
public:
116109
int numRookCaptures(vector<vector<char>>& board) {
117-
vector<int> pos = find(board);
118-
int ans = 0, n = 8;
119-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
120-
for (auto& dir : dirs) {
121-
int x = pos[0], y = pos[1], a = dir[0], b = dir[1];
122-
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') {
123-
x += a;
124-
y += b;
125-
if (board[x][y] == 'p') {
126-
++ans;
127-
break;
128-
}
129-
}
130-
}
131-
return ans;
132-
}
133-
134-
vector<int> find(vector<vector<char>>& board) {
135-
int n = 8;
136-
for (int i = 0; i < n; ++i) {
137-
for (int j = 0; j < n; ++j) {
110+
int ans = 0;
111+
int dirs[5] = {-1, 0, 1, 0, -1};
112+
for (int i = 0; i < 8; ++i) {
113+
for (int j = 0; j < 8; ++j) {
138114
if (board[i][j] == 'R') {
139-
return {i, j};
115+
for (int k = 0; k < 4; ++k) {
116+
int x = i, y = j;
117+
int a = dirs[k], b = dirs[k + 1];
118+
while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') {
119+
x += a;
120+
y += b;
121+
if (board[x][y] == 'p') {
122+
++ans;
123+
break;
124+
}
125+
}
126+
}
140127
}
141128
}
142129
}
143-
return {};
130+
return ans;
144131
}
145132
};
146133
```
147134
148135
### **Go**
149136
150137
```go
151-
func numRookCaptures(board [][]byte) int {
152-
n := 8
153-
154-
find := func() []int {
155-
for i := 0; i < n; i++ {
156-
for j := 0; j < n; j++ {
157-
if board[i][j] == 'R' {
158-
return []int{i, j}
138+
func numRookCaptures(board [][]byte) (ans int) {
139+
dirs := [5]int{-1, 0, 1, 0, -1}
140+
for i := 0; i < 8; i++ {
141+
for j := 0; j < 8; j++ {
142+
if board[i][j] == 'R' {
143+
for k := 0; k < 4; k++ {
144+
x, y := i, j
145+
a, b := dirs[k], dirs[k+1]
146+
for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' {
147+
x, y = x+a, y+b
148+
if board[x][y] == 'p' {
149+
ans++
150+
break
151+
}
152+
}
159153
}
160154
}
161155
}
162-
return []int{}
163-
}
164-
165-
pos := find()
166-
ans := 0
167-
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
168-
for _, dir := range dirs {
169-
x, y, a, b := pos[0], pos[1], dir[0], dir[1]
170-
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != 'B' {
171-
x += a
172-
y += b
173-
if board[x][y] == 'p' {
174-
ans++
175-
break
176-
}
177-
}
178156
}
179-
return ans
157+
return
180158
}
181159
```
182160

0 commit comments

Comments
(0)

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