14822 - Tic Tac Toe Winner   

Description

This program simulates a simple tic-tac-toe (圈圈叉叉) game.
We use numbers 1–9 to represent the positions on the tic-tac-toe board as follows:

1 2 3
4 5 6
7 8 9

Each move is represented by one of these numbers.
The input is a sequence of up to nine integers, indicating the order in which two players make their moves.
Player 1 moves first, followed by Player 2, and so on.

That is, the values in the odd positions of the input sequence represent player 1’s moves, and those in the even positions represent player 2’s moves.

If at any point a move is illegal (that is, a player attempts to play in a cell that is already occupied), the program should immediately output:

illegal move

If one of the players wins, the program should output the number of the last move made by the winning player.

The test cases for this problem guarantee that there are only two possibilities:

  1. One player wins, or

  2. An illegal move occurs.

No tie (沒有和局) or simultaneous win conditions will appear.

 

Reference structure and function definitions:

The following C code structure and function prototypes are provided as a reference framework:

typedef struct {
    int board[3][3];
} TicTacToe;

void init (TicTacToe *ttt);
int play (TicTacToe *ttt, int color, int x, int y);
int win (TicTacToe *ttt, int color);

Function descriptions:

  • init initializes the Tic Tac Toe board by setting all cells to 0.

  • play attempts to place a piece of the specified color at board[x][y].

    • If successful, the position is filled and the function returns 0.

    • If the cell is already occupied, it returns -1.

  • win checks if the given color has achieved a winning condition (three in a row).

    • Returns 1 if that color wins, otherwise returns 0.

Input

A line containing between 1 and 9 integers, representing the sequence of moves in a tic-tac-toe game.

Output

If an illegal move occurs, print

illegal move

Otherwise, print the number (1–9) corresponding to the last move made by the winning player.

Ensure that the output, including formatting, exactly matches the provided samples.

Sample Input  Download

Sample Output  Download

Tags

11410EE 231002



Discuss