@@ -80,11 +80,10 @@ tags:
80
80
81
81
### 方法一:模拟
82
82
83
- 我们先遍历棋盘,找到车的位置 $(x, y )$,然后从 $(x, y )$ 出发,向上下左右四个方向遍历:
83
+ 我们先遍历棋盘,找到车的位置 $(i, j )$,然后从 $(i, j )$ 出发,向上下左右四个方向遍历:
84
84
85
- - 如果遇到象或者边界,那么该方向停止遍历;
86
- - 如果遇到卒,那么答案加一,然后该方向停止遍历;
87
- - 否则,继续遍历。
85
+ - 如果不是边界且不是象,则继续向前走;
86
+ - 如果是卒,则答案加一,并停止该方向的遍历。
88
87
89
88
遍历完四个方向后,即可得到答案。
90
89
@@ -97,50 +96,49 @@ tags:
97
96
``` python
98
97
class Solution :
99
98
def numRookCaptures (self , board : List[List[str ]]) -> int :
100
- ans = 0
101
99
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):
104
103
if board[i][j] == " R" :
104
+ ans = 0
105
105
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" :
109
108
if board[x][y] == " p" :
110
109
ans += 1
111
110
break
112
- if board[x][y] == " B" :
113
- break
114
- return ans
111
+ x, y = x + a, y + b
112
+ return ans
115
113
```
116
114
117
115
#### Java
118
116
119
117
``` java
120
118
class Solution {
121
119
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
+ final int [] 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) {
126
124
if (board[i][j] == ' R' ) {
125
+ int ans = 0 ;
127
126
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' ) {
134
129
if (board[x][y] == ' p' ) {
135
130
++ ans;
136
131
break ;
137
132
}
133
+ x += dirs[k];
134
+ y += dirs[k + 1 ];
138
135
}
139
136
}
137
+ return ans;
140
138
}
141
139
}
142
140
}
143
- return ans ;
141
+ return 0 ;
144
142
}
145
143
}
146
144
```
@@ -151,27 +149,28 @@ class Solution {
151
149
class Solution {
152
150
public:
153
151
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) {
158
156
if (board[ i] [ j ] == 'R') {
157
+ int ans = 0;
159
158
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') {
165
161
if (board[ x] [ y ] == 'p') {
166
162
++ans;
167
163
break;
168
164
}
165
+ x += dirs[ k] ;
166
+ y += dirs[ k + 1] ;
169
167
}
170
168
}
169
+ return ans;
171
170
}
172
171
}
173
172
}
174
- return ans ;
173
+ return 0 ;
175
174
}
176
175
};
177
176
```
@@ -180,25 +179,93 @@ public:
180
179
181
180
```go
182
181
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
+ }
202
269
}
203
270
```
204
271
0 commit comments