-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
26 lines (26 loc) · 902 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int numRookCaptures(char[][] board) {
final int[] dirs = {-1, 0, 1, 0, -1};
int n = board.length;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'R') {
int ans = 0;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') {
if (board[x][y] == 'p') {
++ans;
break;
}
x += dirs[k];
y += dirs[k + 1];
}
}
return ans;
}
}
}
return 0;
}
}