61
61
<li>只有一个格子上存在 <code>board[i][j] == 'R'</code></li>
62
62
</ol >
63
63
64
-
65
64
## 解法
66
65
67
66
<!-- 这里可写通用的实现逻辑 -->
77
76
``` python
78
77
class Solution :
79
78
def numRookCaptures (self , board : List[List[str ]]) -> int :
80
-
81
- def search (board , i , j , direction ):
82
- while i >= 0 and i < 8 and j >= 0 and j < 8 :
83
- if board[i][j] == ' B' : return 0
84
- if board[i][j] == ' p' : return 1
85
- i += direction[0 ]
86
- j += direction[1 ]
87
- return 0
88
- directions = [(- 1 , 0 ), (1 , 0 ), (0 , - 1 ), (0 , 1 )]
89
- res = 0 ;
90
- for i in range (8 ):
91
- for j in range (8 ):
79
+ x, y, n = 0 , 0 , 8
80
+ for i in range (n):
81
+ for j in range (n):
92
82
if board[i][j] == ' R' :
93
- for direction in directions:
94
- res += search(board, i, j, direction)
95
- return res
83
+ x, y = i, j
84
+ break
85
+ 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
93
+ return ans
96
94
```
97
95
98
96
### ** Java**
@@ -102,30 +100,112 @@ class Solution:
102
100
``` java
103
101
class Solution {
104
102
public int numRookCaptures (char [][] board ) {
105
- int [][] directions = { {- 1 , 0 }, {1 , 0 }, {0 , - 1 }, {0 , 1 } };
106
- int res = 0 ;
107
- for (int i = 0 ; i < 8 ; ++ i) {
108
- for (int j = 0 ; j < 8 ; ++ j) {
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 (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != ' B' ) {
109
+ x += a;
110
+ y += b;
111
+ if (board[x][y] == ' p' ) {
112
+ ++ ans;
113
+ break ;
114
+ }
115
+ }
116
+ }
117
+ return ans;
118
+ }
119
+
120
+ private int [] find (char [][] board ) {
121
+ int n = 8 ;
122
+ for (int i = 0 ; i < n; ++ i) {
123
+ for (int j = 0 ; j < n; ++ j) {
109
124
if (board[i][j] == ' R' ) {
110
- for (int [] direction : directions) {
111
- res += search(board, i, j, direction);
112
- }
113
- return res;
125
+ return new int []{i, j};
126
+ }
127
+ }
128
+ }
129
+ return null ;
130
+ }
131
+ }
132
+ ```
133
+
134
+ ### ** C++**
135
+
136
+ ``` cpp
137
+ class Solution {
138
+ public:
139
+ int numRookCaptures(vector<vector<char >>& board) {
140
+ vector<int > pos = find(board);
141
+ int ans = 0, n = 8;
142
+ vector<vector<int >> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
143
+ for (auto& dir : dirs)
144
+ {
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
+ {
148
+ x += a;
149
+ y += b;
150
+ if (board[ x] [ y ] == 'p')
151
+ {
152
+ ++ans;
153
+ break;
114
154
}
115
155
}
116
156
}
117
- return res ;
157
+ return ans ;
118
158
}
119
159
120
- private int search (char [][] board , int i , int j , int [] direction ) {
121
- while (i >= 0 && i < 8 && j >= 0 && j < 8 ) {
122
- if (board[i][j] == ' B' ) return 0 ;
123
- if (board[i][j] == ' p' ) return 1 ;
124
- i += direction[0 ];
125
- j += direction[1 ];
160
+ vector<int> find(vector<vector<char>>& board) {
161
+ int n = 8;
162
+ for (int i = 0; i < n; ++i)
163
+ {
164
+ for (int j = 0; j < n; ++j)
165
+ {
166
+ if (board[i][j] == 'R')
167
+ {
168
+ return {i, j};
169
+ }
170
+ }
126
171
}
127
- return 0 ;
172
+ return {} ;
128
173
}
174
+ };
175
+ ```
176
+
177
+ ### ** Go**
178
+
179
+ ``` go
180
+ func numRookCaptures (board [][]byte ) int {
181
+ n := 8
182
+
183
+ find := func () []int {
184
+ for i := 0 ; i < n; i++ {
185
+ for j := 0 ; j < n; j++ {
186
+ if board[i][j] == ' R' {
187
+ return []int {i, j}
188
+ }
189
+ }
190
+ }
191
+ return []int {}
192
+ }
193
+
194
+ pos := find ()
195
+ ans := 0
196
+ dirs := [4 ][2 ]int {{0 , -1 }, {0 , 1 }, {1 , 0 }, {-1 , 0 }}
197
+ for _ , dir := range dirs {
198
+ x , y , a , b := pos[0 ], pos[1 ], dir[0 ], dir[1 ]
199
+ for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n && board[x+a][y+b] != ' B' {
200
+ x += a
201
+ y += b
202
+ if board[x][y] == ' p' {
203
+ ans++
204
+ break
205
+ }
206
+ }
207
+ }
208
+ return ans
129
209
}
130
210
```
131
211
0 commit comments