Skip to content

Commit

Permalink
Create 1958-check-if-move-is-legal.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
AP-Repositories authored Dec 30, 2022
1 parent 2d0e0cc commit 20d28dc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cpp/1958-check-if-move-is-legal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};

0 comments on commit 20d28dc

Please sign in to comment.