Algorithm


C. Tic-tac-toe
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.

You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:

  • illegal — if the given board layout can't appear during a valid game;
  • the first player won — if in the given board layout the first player has just won;
  • the second player won — if in the given board layout the second player has just won;
  • draw — if the given board layout has just let to a draw.
Input

The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).

Output

Print one of the six verdicts: firstsecondillegalthe first player wonthe second player won or draw.

Examples
input
Copy
X0X
.0.
.X.
output
Copy
second



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <iostream>
#include <vector>

bool winState(char w, std::vector<std::string> matrix){

    if(matrix[0][0] == w && matrix[0][1] == w && matrix[0][2] == w){return 1;}
    if(matrix[1][0] == w && matrix[1][1] == w && matrix[1][2] == w){return 1;}
    if(matrix[2][0] == w && matrix[2][1] == w && matrix[2][2] == w){return 1;}

    if(matrix[0][0] == w && matrix[1][0] == w && matrix[2][0] == w){return 1;}
    if(matrix[0][1] == w && matrix[1][1] == w && matrix[2][1] == w){return 1;}
    if(matrix[0][2] == w && matrix[1][2] == w && matrix[2][2] == w){return 1;}

    if(matrix[0][0] == w && matrix[1][1] == w && matrix[2][2] == w){return 1;}
    if(matrix[0][2] == w && matrix[1][1] == w && matrix[2][0] == w){return 1;}

    return 0;

}


int main(){
    
    std::vector<std::string> game;
    std::string first;  getline(std::cin, first);  game.push_back(first);
    std::string second; getline(std::cin, second); game.push_back(second);
    std::string third;  getline(std::cin, third);  game.push_back(third);

    int numX(0), numO(0);
    for(int row = 0; row < 3; row++){
        for(int col = 0; col < 3; col++){
            if(game[row][col] == 'X'){++numX;}
            else if(game[row][col] == '0'){++numO;}
        }
    }
    

    if(numX > numO + 1 || numO > numX){puts("illegal");}
    else if(winState('X', game) && winState('0', game)){puts("illegal");}
    else if(winState('X', game) && numX != numO + 1){puts("illegal");}
    else if(winState('0', game) && numX != numO){puts("illegal");}
    else if(winState('X', game) && !winState('0', game)){puts("the first player won");}
    else if(winState('0', game) && !winState('X', game)){puts("the second player won");}
    else if(numX == numO + 1 && numX < 5){puts("second");}
    else if(numX == numO){puts("first");}
    else if(numX == 5 && numO == 4){puts("draw">;}

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
X0X
.0.
.X.

Output

x
+
cmd
second
Advertisements

Demonstration


Codeforces Solution-C. Tic-tac-toe-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+