Two players compete on an 8x8 board using crimson discs (X) and azure discs (O); some cells are still empty (.). A move is legal only if placing your disc on an empty cell creates, in at least one of the 8 compass directions (horizontal, vertical, or diagonal), an unbroken straight run of one or more discs of the opposing color immediately followed by a disc of your own color, with no empty cell interrupting that run before your disc is reached. Given the current board, a candidate cell, and the color to be placed there, determine whether the move is legal.
{X, O, .}, describing the board (row 0 first, row 7 last).r and c (the candidate cell's 0-indexed row and column) and a single character color (X or O), the color to be placed there.Print YES if the move is legal, otherwise print NO.
.).color is either X or O.Example 1
Input
........ ........ ........ ........ ....OX.. ........ ........ ........ 4 3 X
Expected
YES
Explanation
Placing X at (4,3) and looking right along row 4: (4,4) holds the opposing O disc, and immediately after it (4,5) holds an X disc of the same color, with no empty cell in between. That single direction is enough to make the move legal, so the answer is YES.
Example 2
Input
........ ........ ........ ........ ....OX.. ........ ........ ........ 0 0 X
Expected
NO
Explanation
The candidate cell (0,0) is far from the discs at (4,4) and (4,5): in every one of the 8 directions from (0,0), the adjacent cells are either empty or immediately run off the board, so no direction ever reaches an opposing disc followed by a same-color disc. The move is illegal, so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →