-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path999.cpp
More file actions
42 lines (42 loc) · 1.24 KB
/
Copy path999.cpp
File metadata and controls
42 lines (42 loc) · 1.24 KB
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
class Solution {
public:
int numRookCaptures(vector<vector<char>> &board) {
int x = 0, y = 0, m = board.size(), n = m != 0 ? board[0].size() : 0, total = 0;
for (int i = 0; i < m; ++i) {
int j = 0;
for (j = 0; j < n; ++j) {
if (board[i][j] == 'R') {
x = i, y = j;
break;
}
}
if (j != n)
break;
}
for (int i = x - 1; i >= 0; --i) {
if (board[i][y] == 'p')
++total;
if (board[i][y] == 'B' || board[i][y] == 'p')
break;
}
for (int i = x + 1; i < m; ++i) {
if (board[i][y] == 'p')
++total;
if (board[i][y] == 'B' || board[i][y] == 'p')
break;
}
for (int j = y - 1; j >= 0; --j) {
if (board[x][j] == 'p')
++total;
if (board[x][j] == 'B' || board[x][j] == 'p')
break;
}
for (int j = y + 1; j < n; ++j) {
if (board[x][j] == 'p')
++total;
if (board[x][j] == 'B' || board[x][j] == 'p')
break;
}
return total;
}
};