forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathavailable-captures-for-rook.cpp
35 lines (32 loc) · 999 Bytes
/
available-captures-for-rook.cpp
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
// Time: O(1)
// Space: O(1)
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
static vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int r = -1, c = -1;
for (int i = 0; i < 8 && r == -1; ++i) {
for (int j = 0; j < 8; ++j) {
if (board[i][j] == 'R') {
tie(r, c) = make_pair(i, j);
break;
}
}
}
int result = 0;
for (const auto& d : directions) {
int nr, nc;
tie(nr, nc) = make_pair(r + d.first, c + d.second);
while (0 <= nr && nr < 8 && 0 <= nc && nc < 8) {
if (board[nr][nc] == 'p') {
++result;
}
if (board[nr][nc] != '.') {
break;
}
tie(nr, nc) = make_pair(nr + d.first, nc + d.second);
}
}
return result;
}
};