#1958
Check if Move is Legal
specialist · 925 · lc medium +32 · premium · verified · 50% accepted · 181 likes · top 38%
Description
You are given an 8 x 8 game board where '.' is empty, 'W' is white, and 'B' is black. A move at a free cell is legal if it creates a good line: a straight line (horizontal, vertical, or diagonal) of three or more cells where both endpoints match your color and all interior cells are strictly the opposite color (no empty cells in between).
Given rMove, cMove, and color, return true if placing color at position (rMove, cMove) is a legal move.
Example 1:
Input: board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
Output: true
Explanation: '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'.
The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.
Example 2:
Input: board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
Output: false
Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.
Code
1
2
3