-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailable-captures-for-rook.h
62 lines (56 loc) · 1.58 KB
/
available-captures-for-rook.h
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
#ifndef AVAILABLE_CAPTURES_FOR_ROOK_H_
#define AVAILABLE_CAPTURES_FOR_ROOK_H_
#include <vector>
namespace solution {
int numRookCaptures(std::vector<std::vector<char>>& board) {
// direct
// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Available Captures for Rook.
// Memory Usage: 6.8 MB, less than 70.73% of C++ online submissions for Available Captures for Rook.
//
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
if (board[i][j] == 'R') {
auto line{i}, column{j}, result{0};
while (--line > -1) {
if (board[line][column] == 'B') {
break;
} else if (board[line][column] == 'p') {
++result;
break;
}
}
line = i, column = j;
while (++line < board.size()) {
if (board[line][column] == 'B') {
break;
} else if (board[line][column] == 'p') {
++result;
break;
}
}
line = i, column = j;
while (--column > -1) {
if (board[line][column] == 'B') {
break;
} else if (board[line][column] == 'p') {
++result;
break;
}
}
line = i, column = j;
while (++column < board[i].size()) {
if (board[line][column] == 'B') {
break;
} else if (board[line][column] == 'p') {
++result;
break;
}
}
return result;
}
}
}
return 0;
}
}
#endif // AVAILABLE_CAPTURES_FOR_ROOK_H_