#P17093. [ICPC 2017 Qingdao R] Chenchen, Tangtang and ZengZeng

[ICPC 2017 Qingdao R] Chenchen, Tangtang and ZengZeng

Problem Description

Chenchen, Tangtang and ZengZeng are starting a game of tic-tac-toe, played on a 3×33 \times 3 board.

Initially, all squares on the board are empty and they takes turns writing the first letter of their name into any of the empty squares (because Chenchen, Tangtang and ZengZeng are elites, their names have different first letters, 'C', 'T' and 'Z' respectively). The game ends when anyone of them places 3 of his letters consecutively in a row, column or diagonally. That people is declared the winner.

Write a program that, given the state of the board, determines if the game is over and who won if it is.

Input Format

The input contains several test cases up to 1000. Each case contains three lines describing the board. Each line contains 3 characters. The characters will be uppercase letters of {C,T,Z}\{'C', 'T', 'Z'\} or '.' (if the square is empty). The data will be such that there is at most one winner.

Output Format

For each case, if the game is over, output the first letter of the winner's name. If not, output "ongoing" even if the board if full.

4
CTZ
TCZ
CTZ
ZCT
CZC
TTZ
.C.
C.T
Z..
CTZ
.C.
CTZ
Z
Z
ongoing
ongoing