forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1958-check-if-move-is-legal.go
Accepted submission: _https://leetcode.com/submissions/detail/871351098/_
- Loading branch information
1 parent
05441d3
commit 311a675
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const ROW = 0 | ||
const COL = 1 | ||
func checkMove(board [][]byte, rMove int, cMove int, color byte) bool { | ||
ROWS, COLS := len(board), len(board[0]) | ||
direction := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, | ||
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}} | ||
board[rMove][cMove] = color | ||
|
||
legal := func(row, col int, color byte, direc []int) bool { | ||
dr, dc := direc[ROW], direc[COL] | ||
row, col = row + dr, col + dc | ||
length := 1 | ||
|
||
for 0 <= row && row < ROWS && 0 <= col && col < COLS { | ||
length += 1 | ||
if board[row][col] == '.' { | ||
return false | ||
} else if(board[row][col] == color) { | ||
return length >= 3 | ||
} | ||
row, col = row + dr, col + dc | ||
} | ||
return false | ||
} | ||
|
||
for _, d := range direction { | ||
if legal(rMove, cMove, color, d) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |