TOE1 - Tic-Tac-Toe ( I )

Tic Tac Toe is a child's game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player's symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.

We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.

subir imagenes

Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?


The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.


For each case print "yes" or "no" on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.





hide comments
tsunami_98: 2019-07-01 17:30:30

Great Problem ...thanks

ahmed ashry: 2019-02-23 00:51:21

- Simple BFS will cover it.
- you have to check both winning case and similarity case to speed up!!

Last edit: 2019-02-23 00:51:38
cod_r: 2018-11-09 15:23:05

Do a quick bfs over it!

vvector: 2018-09-28 14:16:43

Solution is very simple. i used only number of X and O and also number of winning lines.

sinersnvrsleep: 2018-04-02 14:20:24

nice question we are over complicating it it has a simple solution

shahin mahmud: 2018-01-15 12:49:31

you should know
=> X player plays first
=>if X wins then O can't play anymore, same for when O wins
=>cases, X wins, O wins, Both wins, None wins

cake_is_a_lie: 2017-03-07 11:47:13

To the people complaining there are a lot of corner cases, I will say this: there is an elegant solution that is very simple to implement; if you have a lot of corner cases, you're over-complicating it.

Akash Garg: 2016-05-28 08:55:06

Instead of checking whether a configuration is valid, just play the game once and store all possible configurations in a map. O(1) time for checking if something is valid or not. And just few thousand possible configurations possible.

enigmus: 2016-02-25 13:15:29

I would advise people not to pursue this problem. It's easy to think of the solution but annoying to implement. There are so many possible exception one has to be aware about to get AC

minhthai: 2016-01-15 09:45:57

just be careful :)

Added by:Camilo Andrés Varela León
Time limit:0.161s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Waterloo local 2002.09.21