Problem:


Biased Dice

Professor Random, known for his research on randomized algorithms, is now conducting an experiment on biased dice.  His experiment consists of dropping a number of dice onto a plane, one after another from a fixed position above the plane.  The dice fall onto the plane or dice already there, without rotating, and may roll and fall according to their property.  Then he observes and records the status of the stack formed on the plane, specifically, how many times each number appears on the faces visible from above. All the dice have the same size and their face numbering is identical, which we show in Figure C-1.

Figure C-1: Numbering of a die

The dice have very special properties, as in the following.

(1) Ordinary dice can roll in four directions, but the dice used in this experiment never roll in the directions of faces 1, 2 and 3; they can only roll in the directions of faces 4, 5 and 6.  In the situation shown in Figure C-2, our die can only roll to one of two directions.

Figure C-2: An ordinary die and a biased die

(2) The die can only roll when it will fall down after rolling, as shown in Figure C-3.  When multiple possibilities exist, the die rolls towards the face with the largest number among those directions it can roll to.

Figure C-3: A die can roll only when it can fall

(3) When a die rolls, it rolls exactly 90 degrees, and then falls straight down until its bottom face touches another die or the plane, as in the case [B] or [C] of Figure C-4.

(4) After rolling and falling, the die repeatedly does so according to the rules (1) to (3) above.

Figure C-4: Example stacking of biased dice

For example, when we drop four dice all in the same orientation, 6 at the top and 4 at the front, then a stack will be formed as shown in Figure C-4.

Figure C-5: Example records

After forming the stack, we count the numbers of faces with 1 through 6 visible from above and record them.  For example, in the left case of Figure C-5, the record will be "0 2 1 0 0 0", and in the right case, "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 and is the number of the dice to be dropped.  ti and fi (1 ≤ ti, fi ≤ 6) are two integers separated by a space and represent the numbers on the top and the front faces of the i-th die, when it is released, respectively.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output six integers separated by a space. The integers represent the numbers of faces with 1 through 6 correspondingly, visible from above. There may not be any other characters in the output.

Sample Input

4
6 4
6 4
6 4
6 4
2
5 3
5 3
8
4 2
4 2
4 2
4 2
4 2
4 2
4 2
4 2
6
4 6
1 5
2 3
5 3
2 4
4 2
0

Output for the Sample Input

0 1 1 0 0 1
1 0 0 0 1 0
1 2 2 1 0 0
2 3 0 0 0 0



Sample input:
4
6 4
6 4
6 4
6 4
2
5 3
5 3
8
4 2
4 2
4 2
4 2
4 2
4 2
4 2
4 2
6
4 6
1 5
2 3
5 3
2 4
4 2
0


Sample output:
0 1 1 0 0 1
1 0 0 0 1 0
1 2 2 1 0 0
2 3 0 0 0 0

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class Dice():
    def __init__(self, top, front):
        # [top, front, bottom, rear, right, left]
        self.A = [top, front, 7 - top, 7 - front]
        for i in range(4):
            t = (self.A[i-1], self.A[i])
            if t == (6, 4):
                self.A += [5, 2]
            elif t == (4, 6):
                self.A += [2, 5]
            elif t == (6, 5):
                self.A += [3, 4]
            elif t == (6, 2):
                self.A += [4, 3]
            elif t == (5, 4):
                self.A += [1, 6]
            elif t == (5, 3):
                self.A += [6, 1]
            else:
                continue
            break

    def top(self):
        return self.A[0]

    def front(self):
        return self.A[1]

    def bottom(self):
        return self.A[2]

    def rear(self):
        return self.A[3]

    def right(self):
        return self.A[4]

    def left(self):
        return self.A[5]

    # di: 0 == front, 1 == rear, 2 == right, 3 == left
    def rotate(self, di):
        a = self.A
        if di == 0:
            self.A = [a[3], a[0], a[1], a[2], a[4], a[5]]
        elif di == 1:
            self.A = [a[1], a[2], a[3], a[0], a[4], a[5]]
        elif di == 2:
            self.A = [a[5], a[1], a[4], a[3], a[0], a[2]]
        elif di == 3:
            self.A = [a[4], a[1], a[5], a[3], a[2], a[0]]

def main():
    rr = []

    while True:
        n = I()
        if n == 0:
            break

        a = [LI() for _ in range(n)]
        rb = collections.defaultdict(int)
        b = collections.defaultdict(int)
        for t,f in a:
            ki = 0
            kj = 0
            d = Dice(t,f)
            f = True
            while f:
                f = False
                bt = b[(ki,kj)]
                for i in range(6,3,-1):
                    if d.front() == i and bt > b[(ki-1,kj)]:
                        f = True
                        d.rotate(0)
                        ki -= 1
                        break
                    if d.rear() == i and bt > b[(ki+1,kj)]:
                        f = True
                        d.rotate(1)
                        ki += 1
                        break
                    if d.right() == i and bt > b[(ki,kj+1)]:
                        f = True
                        d.rotate(2)
                        kj += 1
                        break
                    if d.left() == i and bt > b[(ki,kj-1)]:
                        f = True
                        d.rotate(3)
                        kj -= 1
                        break
            b[(ki,kj)] += 1
            rb[(ki,kj)] = d.top()

        r = [0] * 6
        for v in rb.values():
            if v == 0:
                continue
            r[v-1] += 1

        rr.append(' '.join(map(str,r)))

    return '\n'.join(map(str, rr))



print(main())

Submission status: Runtime Error

Solution code:
false=False
true=True

dices={}
me=[0,0,0,0,0,0]
def getDice(x,y,z):
    return dices["{},{},{}".format(x,y,z)]
def setDice(x,y,z,top,ground=False):
    dices["{},{},{}".format(x,y,z)]=top
	me[top]+=1
	if not ground:
		me[getDice(x,y,z-1)]-=1

while True:
    n=int(input())
    if n==0:
        break
    #########
    
    Z=0
    for i in range(n):
        top,fwd=map(int,input().split())
        x=0,y=0,z=Z+1
        while True:
            if z==0 :
                setDice(x,y,z,top,True)
                break
			never=True
            def canContinue(smx,smy,lgx,lgy):
				small=getDice(x+smx,y+smy,z-1)
				large=getDice(x+lgx,y+lgy,z-1)
				if large is　not None:
					x+=lgx
					y-=lgy
					z-=1
					never=False
					return True
				elif small is not None:
					x+=smx
					y+=smy
					z-=1
					return True
					never=False
				else:
					return False
			
            if top==1:
                if fwd==2:
					if canContinue(-1,0,0,-1):
						continue
                elif fwd==3:
                    if canContinue(0,-1,1,0):
						continue
                elif fwd==4:
                    if canContinue(0,1,-1,0):
						continue
				elif fwd==5:
                    if canContinue(1,0,0,1):
						continue
            elif top==2:
                if fwd==1:
					if canContinue(1,0,0,-1):
						continue
                elif fwd==3:
                    if canContinue(0,-1,-1,0):
						continue
                elif fwd==4:
                    if canContinue(0,1,1,0):
						continue
				elif fwd==6:
                    if canContinue(-1,0,0,1):
						continue
			elif top==3:
                if fwd==1:
					if canContinue(-1,0,0,-1):
						continue
                elif fwd==2:
                    if canContinue(0,-1,1,0):
						continue
                elif fwd==5:
                    if canContinue(0,1,-1,0):
						continue
				elif fwd==6:
                    if canContinue(1,0,0,1):
						continue
			elif top==4:
                if fwd==1:
					if canContinue(1,0,0,-1):
						continue
                elif fwd==2:
                    if canContinue(0,-1,-1,0):
						continue
                elif fwd==5:
                    if canContinue(0,1,1,0):
						continue
				elif fwd==6:
                    if canContinue(-1,0,0,1):
						continue
			elif top==5:
                if fwd==1:
					if canContinue(-1,0,0,-1):
						continue
                elif fwd==3:
                    if canContinue(0,-1,1,0):
						continue
                elif fwd==4:
                    if canContinue(0,1,-1,0):
						continue
				elif fwd==6:
                    if canContinue(1,0,0,1):
						continue
			elif top==6:
                if fwd==2:
					if canContinue(1,0,0,-1):
						continue
                elif fwd==3:
                    if canContinue(0,-1,-1,0):
						continue
                elif fwd==4:
                    if canContinue(0,1,1,0):
						continue
				elif fwd==5:
                    if canContinue(-1,0,0,1):
						continue
			setDice(x,y,z,top)
			if never:
				Z+=1
			break
		
	print(" ".join(me))