Tic-tac-toe
InterviewTime limit5sMemory limit512 MB
For each 3x3 tic-tac-toe board, decide whether it is invalid, reachable only by non-optimal play, or reachable by two perfect players.
- Level
Medium7 of 10
- Topics
- Game theory, Simulation, Implementation, Brute force
- Solved
- No attempts yet
Problem
A group of archaeologists has found an ancient clay tablet with a game of Tic-tac-toe engraved on it.
Tic-tac-toe is a paper-and-pencil game for two players, 'O' and 'X', who take turns marking the spaces in a grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.
You, as an employee of the State Historical Museum in Byteozavodsk, are to arbitrate if this state of game could have been created by two excellent players.
Input
The first line of input contains a single positive integer , the number of test cases. The descriptions of the test cases follow.
Each test case consists of three lines, three characters on each line. The -th character of the -th line denotes the state of the -th square in the -th row of the clay tablet. There are three possibilities:
- "
." denotes an empty square, - "
O" (big "o") denotes a square that was marked by first player, - "
X" denotes a square that was marked by second player.
Each test case is preceded by a single blank line.
Output
For each test case, output a single line containing a single word: "INVALID", if there does not exist a valid sequence of alternating moves that leads to this game state, "UNREACHABLE", if there does exist a valid sequence of alternating moves that leads to this game state, but only when at least one of players is not excellent, and "REACHABLE" otherwise.
Hint
Provided that it is possible, an excellent player always performs a move that lets him win regardless of further moves of his opponent. If it is not possible, then he performs a move that brings him to a draw. If worse comes to worst, he makes any move.