Tiny - 2
Time limit1sMemory limit512 MB
Given a sequence of unrotatable Tiny pieces, choose a column for each so that all N pieces fit inside a 9 by 9 container without exceeding its top.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Simulation, Brute force
- Solved
- No attempts yet
Problem
Elder people still remember the famous computer game "TETRIS" created by Alexey Pajitnov, where pieces made of four squares (tetrominoes) fall from the sky. The goal is to rotate and land every piece in a rectangular container so as to create as many lines of blocks without gaps as possible. When such lines are created, they disappear, giving more space for the following pieces.
Let's investigate a simpler version of the game, called "Tiny TETRIS" (or just "Tiny" for short). There are only nine different Tiny pieces (or t-pieces) consisting of one to three squares:

The number denotes the type of a t-piece and is used to reference that particular t-piece.
The goal of the game is the same: falling t-pieces must be put in a rectangular container that is 9 units wide and 9 units high. Unlike TETRIS, t-pieces cannot be rotated. They also cannot be moved left or right after they start falling. For each t-piece, the player must only choose the column number (an integer from 1 to 9) where the leftmost square of the piece, marked as ×, will fall.
Each game consists of a finite sequence of N t-pieces. As many as possible must be dropped into the container without exceeding its upper level or making an illegal move. The score of the game is the number of successfully dropped t-pieces.
At the beginning, the game counter is set to 0. The algorithm of the game is as follows:
- The player chooses the column for the leftmost square of the current t-piece.
- If the column is set correctly (for example, column 8 can never be correct for t-piece 5), the t-piece falls until it meets an obstacle. Otherwise, the game is over.
- If the t-piece fully fits inside the container (that is, all squares are inside the 9×9 rectangle), the counter increases by one. Otherwise, the game is over.
- Then it is checked whether any horizontal lines are completed (filled completely with blocks of t-pieces without any gaps). If there are, these lines disappear and the lines above them shift down without changing their configuration.
- If any t-pieces are left, proceed to 1). Otherwise, the game is over.
The score of a game is the value of the counter when the game ends.
Let's analyze one particular game.

The sequence of the given 20 t-pieces is 5,4,1,6,7,6,4,4,7,9,5,5,6,8,3,4,3,7,4,2. Suppose the first 17 t-pieces have already been successfully dropped into the container in columns 1,2,2,4,8,8,7,4,8,6,1,1,4,8,3,7,7, respectively. Until this moment no lines have been completed, the counter is 17, and it is time to drop t-piece 7 (letters in the figure are assigned to t-pieces consecutively):
There are only two valid columns where t-piece 7 can be dropped:
You are given five files, each containing a description of a particular game: tiny.i01, tiny.i02, tiny.i03, tiny.i04 and tiny.i05. Each file contains the sequence of t-pieces in this format: the first line contains a single integer N. The next N lines describe the t-piece sequence, each line containing an integer between 1 and 9, the number of the particular t-piece. T-pieces are given in the order they must be dropped into the container; the i-th t-piece is on line i+1 of the file.
For each of the given input files, you must submit a corresponding output file with at most N rows. The i-th row of the output file must contain the column number where the i-th t-piece from the input data must be dropped.
It is guaranteed that for each input file there exists a sequence of columns that allows all t-pieces to be successfully dropped into the container (so that the game score equals N).

