Submit | All submissions | Best solutions | Back to list |
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.
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?
Input
The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.
Output
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.
Example
Input: 2 X.O OO. XXX O.X XX. OOO Output: yes no
Added by: | Camilo Andrés Varela León |
Date: | 2006-12-14 |
Time limit: | 1s |
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 |
hide comments
|
||||||||||
2020-02-20 18:12:30
This one caused me a lot of trouble. 1 XXX OXO XOX no Last edit: 2020-02-20 18:12:40 |
||||||||||
2019-07-01 17:30:30
Great Problem ...thanks |
||||||||||
2019-02-23 00:51:21 ahmed ashry
- 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 |
||||||||||
2018-11-09 15:23:05
Do a quick bfs over it! |
||||||||||
2018-09-28 14:16:43
Solution is very simple. i used only number of X and O and also number of winning lines. |
||||||||||
2018-04-02 14:20:24
nice question we are over complicating it it has a simple solution |
||||||||||
2018-01-15 12:49:31 shahin mahmud
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 |
||||||||||
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. |
||||||||||
2016-05-28 08:55:06 Akash Garg
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. |
||||||||||
2016-02-25 13:15:29 enigmus
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 |