-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1958-check-if-move-is-legal.cpp
30 lines (27 loc) · 1.07 KB
/
1958-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
class Solution {
public:
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
const int ROWS = board.size(), COLS = board[0].size();
int direction[8][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
board[rMove][cMove] = color;
function<bool(int, int, char, int[])> legal = [&] (int row, int col, char color, int direc[]) -> bool {
int dr = direc[0], dc = direc[1];
row = row + dr;
col = col + dc;
int length = 1;
while(0 <= row && row < ROWS && 0 <= col && col < COLS) {
length += 1;
if(board[row][col] == '.') return false;
if(board[row][col] == color)
return length >= 3;
row = row + dr;
col = col + dc;
}
return false;
};
for(auto& d: direction)
if(legal(rMove, cMove, color, d)) return true;
return false;
}
};