Skip to content

Commit 311a675

Browse files
Create 1958-check-if-move-is-legal.go
Accepted submission: _https://leetcode.com/submissions/detail/871351098/_
1 parent 05441d3 commit 311a675

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

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

0 commit comments

Comments
 (0)