CCCHK '08 J4 - Tic-tac-toe

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

The game tic-tac-toe is played on a 3 \times 3 grid. Two players places cross (X) and nought (O) alternatively. The game ends whenever a player can make a line of 3 of their mark vertically, horizontally, or diagonally; or all slots are occupied.

Figure 1: A snapshot of a game.

In this problem, you are given a snapshot of a game and you need to verify if it is valid. You can assume that the first mark to be placed is always a cross.

Input Specification

The first input is a number T specifying the number of test cases (1 \le T \le 20\,000). For each test case, 9 characters on each line are used to denote a snapshot, each containing only . (period, representing an empty slot), X, and O. The first 3 characters are the contents of the first row, the next 3 are for the second row, etc. For example, the snapshot of the example is represented as follows:


Output Specification

For each test case, output yes if the snapshot is valid; no, otherwise.

Sample Input


Sample Output



  • 0
    jeffreylin  commented on May 10, 2020, 11:38 p.m. edited

    There was a lot more cases than I realized, took me a while

  • 1
    Arihan10  commented on Jan. 28, 2019, 3:17 p.m. edited

    Does anyone know why I am failing Test Case 2? I tried many different test cases, and all of them were fine.


  • 1
    IanHu  commented on Dec. 16, 2018, 9:00 p.m.

    No idea why i get WA in test case 2. I think I've covered all situation.....

  • 2
    Phoenix1369  commented on June 24, 2017, 7:30 p.m.

    An error has been discovered in the official test data. The output has been fixed and all submissions have been rejudged. Apologies for any inconvenience caused.

  • 2
    TypicalToxic  commented on June 24, 2017, 4:51 p.m.

    An admin should revisit this problem, 0 solution?

    • 6
      r3mark  commented on June 24, 2017, 6:16 p.m.

      d WA'd, something must be wrong.

  • 3
    Noam527  commented on May 11, 2017, 8:02 a.m.

    I'm pretty sure "valid" as if it's possible to reach this state from the beginning... not that hard to understand

  • 4
    ASentientBot  commented on April 25, 2017, 11:03 a.m.

    Does "valid" just mean that (1) no more than one person has won and (2) there are not more O's than X's?

    • -1
      IanHu  commented on Dec. 16, 2018, 8:41 p.m.

      and also, when "O" is win, number of "X" cannot be greater than "O": "XO. XOX .OX" is invalid

    • 1
      TypicalToxic  commented on May 7, 2017, 11:23 p.m.

      Bump, Clarification on valid?