Available Captures for Rook LeetCode solution in C++

1
class Solution {
2
public:
3
int numRookCaptures(vector<vector<char>> &board) {
4
int res = 0;
5
int p = -1, q = -1;
6
for (int i = 0; i < board.size(); i++) {
7
for (int j = 0; j < board[0].size(); j++) {
8
if (board[i][j] == 'R') // storing position of R
9
{
10
p = i;
11
q = j;
12
break;
13
}
14
}
15
if (p >= 0) break;
16
}
17
// traverse Up, Down, Left and Right from R
18
for (int i = p + 1; i < board.size(); i++) {
19
if (board[i][q] != '.') {
20
if (board[i][q] == 'p') res++;
21
break;
22
}
23
}
24
for (int i = p - 1; i >= 0; i--) {
25
if (board[i][q] != '.') {
26
if (board[i][q] == 'p') res++;
27
break;
28
}
29
}
30

31
for (int j = q + 1; j < board[0].size(); j++) {
32
if (board[p][j] != '.') {
33
if (board[p][j] == 'p') res++;
34
break;
35
}
36
}
37
for (int j = q - 1; j >= 0; j--) {
38
if (board[p][j] != '.') {
39
if (board[p][j] == 'p') res++;
40
break;
41
}
42
}
43
return res;
44
}
45
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0