forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-move-is-legal.cpp
32 lines (30 loc) · 977 Bytes
/
check-if-move-is-legal.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
// Time: O(8 * n) = O(1), grid is a n x n board and n = 8
// Space: O(1)
class Solution {
public:
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
static const vector<pair<int, int>> directions =
{{0, -1}, {0, 1}, {-1, 0}, {1, 0},
{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
for (const auto& [dr, dc] : directions) {
int r = rMove + dr, c = cMove + dc;
if (check(board, color, r, c, dr, dc)) {
return true;
}
}
return false;
}
private:
bool check(vector<vector<char>>& board, char color, int r, int c, int dr, int dc) {
for (int l = 2;
0 <= r && r < size(board) &&
0 <= c && c < size(board[0]) &&
board[r][c] != '.';
r += dr, c += dc, ++l) {
if (board[r][c] == color) {
return l >= 3;
}
}
return false;
}
};