Skip to content

Commit 3ff8265

Browse files
authored
Merge pull request #1781 from AP-Repositories/patch-49
Create 1958-check-if-move-is-legal.cpp
2 parents 21e865e + 20d28dc commit 3ff8265

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cpp/1958-check-if-move-is-legal.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
4+
const int ROWS = board.size(), COLS = board[0].size();
5+
int direction[8][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
6+
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
7+
board[rMove][cMove] = color;
8+
9+
function<bool(int, int, char, int[])> legal = [&] (int row, int col, char color, int direc[]) -> bool {
10+
int dr = direc[0], dc = direc[1];
11+
row = row + dr;
12+
col = col + dc;
13+
int length = 1;
14+
15+
while(0 <= row && row < ROWS && 0 <= col && col < COLS) {
16+
length += 1;
17+
if(board[row][col] == '.') return false;
18+
if(board[row][col] == color)
19+
return length >= 3;
20+
row = row + dr;
21+
col = col + dc;
22+
}
23+
return false;
24+
};
25+
26+
for(auto& d: direction)
27+
if(legal(rMove, cMove, color, d)) return true;
28+
return false;
29+
}
30+
};

0 commit comments

Comments
 (0)