Biased Dice
Time limit1sMemory limit128 MB
Simulate dropping biased dice one by one onto a grid and count which numbers show on the top faces of the final pile.
- Level
Medium7 of 10
- Topics
- Simulation, Implementation, Geometry, Matrix
- Solved
- No attempts yet
Problem
Professor Random, famous for his research on randomized algorithms, is now experimenting with biased dice. In each experiment he drops several dice, one after another, from a fixed position above a plane. A die falls without rotating on its own onto the plane or onto dice already resting there, and it may then roll and fall according to its special properties. Afterwards he observes the resulting pile and records how many times each number appears among the faces visible from above. All dice have the same size and the same face numbering, shown in Figure C-1. Opposite faces always sum to 7 (1↔6, 2↔5, 3↔4).

Figure C-1: Numbering of a die
The dice have the following special properties.
(1) An ordinary die can roll in four directions, but the dice in this experiment never roll toward faces 1, 2 and 3; they can roll only toward faces 4, 5 and 6. In the situation shown in Figure C-2, such a die can roll in only one of two directions.

Figure C-2: An ordinary die and a biased die
(2) A die rolls only when it will fall down after rolling (Figure C-3). When more than one such direction exists, the die rolls toward the direction whose face shows the largest number.

Figure C-3: A die can roll only when it can fall
(3) When a die rolls, it turns exactly 90 degrees and then falls straight down until its bottom face touches another die or the plane, as in case [B] or [C] of Figure C-4.
(4) After rolling and falling, the die keeps repeating this process according to rules (1)-(3) above.

Figure C-4: Example stacking of biased dice
For example, if we drop four dice all in the same orientation (6 on top and 4 in front), the pile in Figure C-4 is formed.

Figure C-5: Example records
Once the pile is complete, we count how many of the faces visible from above show each number from 1 through 6. For instance, the left case of Figure C-5 is recorded as 0 2 1 0 0 0, and the right case as 0 1 1 0 0 1.
Input
The input consists of several datasets, each in the following format.
n
t1 f1
t2 f2
...
tn fn
Here, n (1 ≤ n ≤ 100) is an integer giving the number of dice to be dropped. ti and fi (1 ≤ ti, fi ≤ 6) are two integers separated by a space that give the numbers on the top face and the front face of the i-th die at the moment it is released.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, output six integers separated by single spaces. They give, in order, how many of the faces visible from above show each number from 1 through 6. No other characters may appear in the output.