-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAvailable Captures for Rook.java
68 lines (58 loc) · 1.44 KB
/
Available Captures for Rook.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Runtime: 0 ms (Top 100.00%) | Memory: 41.3 MB (Top 62.26%)
class Solution {
public int numRookCaptures(char[][] board) {
int ans = 0;
int row = 0;
int col = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'R') {
row = i;
col = j;
break;
}
}
}
int j = col;
while (j >= 0) {
if (board[row][j] == 'B') {
break;
} else if (board[row][j] == 'p') {
ans++;
break;
}
j--;
}
j = col;
while (j <= board[0].length - 1) {
if (board[row][j] == 'B') {
break;
} else if (board[row][j] == 'p') {
ans++;
break;
}
j++;
}
int i = row;
while (i <= board.length - 1) {
if (board[i][col] == 'B') {
break;
} else if (board[i][col] == 'p') {
ans++;
break;
}
i++;
}
i = row;
while (i >= 0) {
if (board[i][col] == 'B') {
break;
} else if (board[i][col] == 'p') {
ans++;
break;
}
i--;
}
return ans;
}
}