Problem Description
Given an 8x8 board with cells that can be free ('.'), white ('W'), or black ('B'), determine if placing a stone of a given color at a free cell (rMove, cMove) is legal. A move is legal if the placed stone becomes an endpoint of at least one "good line". A good line (horizontal, vertical, or diagonal) is a contiguous line of three or more cells where both endpoints are the same color (equal to the player's color) and all cells in between are of the opponent’s color (with no free cells).
Key Insights
- You must check in all eight directions (horizontal, vertical, and diagonal) from the move cell.
- In each direction, the immediate neighbor must be of the opponent's color.
- Continue along the direction until you either run off the board, hit a free cell, or encounter a cell of the player's color.
- A direction forms a good line if at least one opponent cell is encountered before finding a player's color cell.
- Since the board is fixed at 8x8, each direction check is constant time.
Space and Time Complexity
Time Complexity: O(1) (since the board size is fixed at 8x8 and each direction is checked with a constant number of steps) Space Complexity: O(1) (only a few extra variables are used)
Solution
The solution iterates over all eight possible directions from the move location. For each direction, it first checks whether the adjacent cell is of the opponent’s color; if not, that direction is immediately discarded. If the adjacent cell is valid, it continues moving in that direction, collecting only opponent pieces until a cell of the player's color is encountered. If such a cell is found and at least one opponent cell was between the new stone and that endpoint, the move is legal via that direction. If at least one direction results in a valid good line, return true; otherwise, return false.