Pousse

Time limit1sMemory limit128 MB

Summary
Simulate the push game on an N by N board and report which color first has more complete rows or columns, or a tie at QUIT.
Level

Medium4 of 10

Topics
Simulation, Implementation, Matrix, Game theory
Solved
No attempts yet

Problem

Pousse (French for "push") is a two-player game played on an N×NN \times N board. The two players, XX and OO, take turns inserting one marker of their own color; XX always moves first. The board starts empty. Rows and columns are numbered from 11 to NN, starting at the top-left corner:

   1 2 3 4
  +-+-+-+-+
1 | | | | |
  +-+-+-+-+
2 | | | | |
  +-+-+-+-+
3 | | | | |
  +-+-+-+-+
4 | | | | |
  +-+-+-+-+

A marker is inserted by sliding it onto a row from the left or the right, or onto a column from the top or the bottom, so there are 4N4N possible moves. They are written Li, Ri, Ti, Bi, meaning insert into row or column i from the Left, Right, Top, or Bottom respectively.

The square where the marker enters is the end square of that row or column (column 11 for Li, column NN for Ri, row 11 for Ti, row NN for Bi). If that square is empty, the marker simply occupies it. Otherwise, all markers from the entry square up to the first empty square of that row or column are moved one square further along to make room, and the new marker takes the entry square. If the row or column has no empty square, the marker at the far end is pushed off the board and removed from play.

A row or a column is a straight of a given color if all NN of its squares hold markers of that color. After each insertion the straights of each color are counted. The moment a move leaves one color with strictly more straights than the other, the game ends and that color's player wins.

Input

The first line contains an integer NN (1≤N≤1001 \le N \le 100). Each of the following lines contains one move, in the notation described above, with no extra spaces or blank lines. Every move in the sequence is valid. The last line of the input is always QUIT.

Output

Play the moves in order. As soon as a move produces a position in which one color has strictly more straights than the other, print X WINS or O WINS (whichever color is dominant) and stop. If the QUIT line is reached before any winner is decided, print TIE GAME.

Examples2

  1. Example 1

    Input
    4
    L2
    T2
    L2
    B2
    R2
    QUIT
    
    Expected output
    TIE GAME
    
  2. Example 2

    Input
    1
    L1
    QUIT
    
    Expected output
    X WINS