Problem:


テトリス

テトリスは、落ちてくるブロックを盤面上に並べて消すゲームです。ここでは、それを少しアレンジしたゲームを考えましょう。

このゲームの盤面の大きさは横 5 コマで、出現するブロックがすべて入るだけの高さがあります。落ちてくるブロックは直線状で、横向き、縦向きの 2 種類があり、長さは 1 から 5 までの 5 種類です。

以下に例を示します。Step(イ) からStep(ホ)までの図はブロックが落ちて消えていく様子を表したものです。
Step(イ)から順にStep(ロ)、Step(ハ)と順に進んでいきます。

  ブロックを落とすときに、Step(イ)のようにブロックのどこかが下に積んであるブロックに引っかかったときには、Step(ロ)のように落ちたブロックはその場所で止まります。また、ブロックを落とした結果、盤面の横一行の全てのコマにブロックが詰まった場合には、Step(ニ)で示されるように、その行のブロックが消えます。この後、消えた行の上にあるブロックが、そのままの形で1行下にしずみます(Step(ホ))。

1 ゲームは 1000 個以内のブロックが順に落ちてくることとします。例えば、落ちるブロックの長さが順番に横向き 4 コマ, 横向き 3 コマ, 縦向き 2 コマ, 縦向き 3 コマで、落ちる場所が左端から 1 コマ目、1 コマ目、4 コマ目、5 コマ目であった場合には、下図のStep(イ)~(ト)のように落ち、最後に残るブロックは 2 コマになります。

 順番に落ちてくるブロックの情報を入力とし、全てのブロックが落ちた時に残るコマ数を出力するプログラムを作成してください。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。

n
d1 p1 q1
d2 p2 q2
:
dn pn qn

１行目にブロックの数 n (1 ≤ n ≤ 1000) が与えられます。続く n 行に i 個目のブロックの向き di (1 または2)、ブロックの長さ pi (1 ≤ pi ≤ 5)、ブロックの位置 qi が与えられます。ブロックの向き di は 1 が横向きを、2 が縦向きを表します。ブロックの位置 qi は盤面上の左端から1 ~ 5 までの整数とし、横向きのブロックの場合は左端のコマの落ちる位置とします。

データセットの数は 20 を超えません。

Output

データセット毎に最後に残るブロックの占めるコマ数を１行に出力します。

Sample Input

4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0

Output for the Sample Input

2
0
6



Sample input:
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0


Sample output:
2
0
6

Submission status: Wrong Answer

Solution code:
def yoko():
    for h, line in enumerate(L):
        if line[q-1:q-1+p] != [' ']*p:
            h = h-1
            break
    for i in range(q-1, q-1+p):
        L[h][i] = '#'
    check(h, 1)
    
def tate():
    for h, line in enumerate(L):
        if line[q-1] != ' ':
            h = h-1
            break
    for i in range(h-p+1, h-p+1+p):
        L[i][q-1] = '#'
    check(h-p+1, p)

def check(h, c):
    while 0 < c:
        if L[h] == ['#']*5:
            del L[h]
        else:
            h += 1
        c -= 1
     
def add():
    c = 0
    for line in L:
        if line == [' ']*5:
            c += 1
        else:
            break
    for i in range(5-c):
        L.insert(0, [' ']*5)
        
def result():
    ans = 0
    for line in L:
        for v in line:
            if v == '#':
                ans += 1
    else:
        print ans

while True:
    n = input()
    if n == 0: break
    L = [[' ']*5 for i in range(5)]
    for line in L:
        print line
    for i in range(n):
        d, p, q = map(int, raw_input().split())
        if d == 1:
            yoko()
        else:
            tate()
        add()
    else:
        result()

Submission status: Wrong Answer

Solution code:
N = int(input())

d, l, p = raw_input().split()
print d, l, p

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if n == 0:
    break

  mp = [[0] * 5 for _ in range(5000)]
  depth = [0, 0, 0, 0, 0]
  ans = 0
  for _ in range(n):
    d, p, q = map(int, input().split())
    ans += p

    if d == 1:
      max_depth = max(depth[q-1:q-1+p])
      for i in range(q - 1, q - 1 + p):
        mp[max_depth][i] = 1
        depth[i] = max_depth + 1
    else:
      max_depth = depth[q - 1]
      for i in range(max_depth, max_depth + p):
        mp[i][q - 1] = 1
      depth[q - 1] = max_depth + p
  
  max_depth = max(depth)
  cnt = 0
  for depth in range(max_depth):
    if mp[depth] == [1,1,1,1,1]:
      cnt += 1
  print(ans - 5 * cnt)

Submission status: Wrong Answer

Solution code:
MAXN=3000
def deleteBlocks():
    i=MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        if set(L[i])==set([True]):
            del L[i]
            L.insert(0,[False]*5)
        else:
            i-=1

def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
    deleteBlocks()

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h+1,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+1)
					break
		else:
			for li in range(h+1,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+q)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(h)])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h+1,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+1)
					break
		else:
			for li in range(h+1,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+q)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(len(field))])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h+2,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+1)
					break
		else:
			for li in range(h+2,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+q)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(len(field))])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				top=[x-1 for x in top if x>=i]
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[h]) for h in range(max(top))])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				top=[x-1 for x in top if x>i]
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[h]) for h in range(max(top))])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				top=[x-1 for x in top]
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[h]) for h in range(max(top))])

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
	count=0
	for i in range(max(top)):
		if field[i]==[1]*5:
			count+=1
	print sum([sum(field[h]) for h in range(max(top))])-5*count

Submission status: Wrong Answer

Solution code:
while 1:
    n=input()
    if n==0:break
    hmax=7500
    block=[map(int,raw_input().split()) for i in range(n)]
    field=[[0]*5 for i in range(hmax)]
    h=0
    for d,p,q in block:
        if d==1:
            for li in range(h,-2,-1):
                if field[li][q-1:q+p-1]!=[0]*p or li==-1:
                    field[li+1][q-1:q+p-1]=[1]*p
                    h=max(h,li+2)
                    break
        else:
            for li in range(h,-2,-1):
                if field[li][q-1]!=0 or li==-1:
                    for i in range(p):
                        field[li+i+1][q-1]=1
                    h=max(h,li+1+p)
                    break
    count=0
    for i in range(h):
        if field[i]==[1]*5:
            count+=1
    print sum([sum(field[li]) for li in range(h)])-5*count

Submission status: Time Limit Exceeded

Solution code:
MAXN=5000
def deleteBlocks():
    i=MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        if set(L[i])==set([True]):
            del L[i]
            L.insert(0,[False]*5)
        else:
            i-=1

def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
    deleteBlocks()

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)

Submission status: Time Limit Exceeded

Solution code:
MAXN=5000
def deleteBlocks():
    i=MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        if set(L[i])==set([True]):
            del L[i]
            L.insert(0,[False]*5)
        else:
            i-=1

def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
    deleteBlocks()

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    cnt,i=0,MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        cnt+=sum(L[i])
        i-=1
    print cnt

Submission status: Time Limit Exceeded

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	for d,p,q in block:
		if d==1:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1:q+p-1]!=[0]*p:
					field[li+1][q-1:q+p-1]=[1]*p
					break
			else:
				field[0][q-1:q+p-1]=[1]*p
		else:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1]!=0:
					for i in range(p):
						field[li+i+1][q-1]=1
					break
			else:
				for i in range(p):
					field[li+i][q-1]=1
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				hmax-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(hmax)])

Submission status: Accepted

Solution code:
end = [1,1,1,1,1]
while True:
  n = int(input())
  if n == 0:
    break
  mp = [[0] * 5 for _ in range(6000)]
  height = [0] * 5
  cnt = 0
  for i in range(n):
    d, p, q = map(int, input().split())
    q -= 1
    cnt += p
    if d == 1:
      pos = max(height[q:q + p])
      mp[pos][q:q + p] = [1] * p
      if mp[pos] == end:
        cnt -= 5
        mp.pop(pos)
        mp.append([0] * 5)
        for x in range(5):
          for y in range(i * 5 - 1, -1, -1):
            if mp[y][x] == 1:
              height[x] = y + 1
              break
          else:
            height[x] = 0
      else:
        height[q:q + p] = [pos + 1] * p

    if d == 2:
      pop_flag = False
      pos = height[q]
      for y in range(pos, pos + p):
        mp[y][q] = 1
      for y in range(pos + p - 1, pos - 1, -1):
        if mp[y] == end:
          cnt -= 5
          mp.pop(y)
          mp.append([0] * 5)
          pop_flag = True
      
      if pop_flag:
        for x in range(5):
          for y in range(i * 5 - 1, -1, -1):
            if mp[y][x] == 1:
              height[x] = y + 1
              break
          else:
            height[x] = 0
      else:
        height[q] += p
  
  print(cnt)

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

YOKO = 1
TATE = 2

def main():
    # input
    while True:
        N = int(raw_input())
        if N == 0:
            break

        data = []
        for x in xrange(N):
            data.append(map(int, raw_input().split()))

        print(restBlock(data))

def restBlock(data):
    state = []
    for x in data:
        if x[0] == YOKO:
            block = makeYokoBlock(x)
            state = yokoBlock(state, block)
        else:
            blocks = makeTateBlocks(x)
            state = tateBlock(state, blocks)

        state = clearTetoris(state)
    return countRestBlock(state)

def clearTetoris(state):
    return filter(lambda x:sum(x) < 5, state)

# Blockが重なっていたらFalse
def checkBlock(a, b):
    if max([sum(x) for x in zip(a, b)]) > 1:
        return False
    else:
        return True

def countRestBlock(state):
    return sum([sum(x) for x in state])

def tateBlock(state, blocks):
    for x in blocks:
        state = yokoBlock(state, x)
    return state

def makeTateBlocks(block_data):
    sckellton = [0, 0, 0, 0, 0]
    i = block_data[2] - 1
    sckellton[i] = 1
    return [sckellton for x in xrange(block_data[1])]

def yokoBlock(state, block):

    if len(state) == 0:
        state.append(block)
        return state

    for i, x in enumerate(state[::-1]):
        if checkBlock(x, block):
            pass
        else:
            if i == 0:
                state.append(block)
            else:
                state[-i] = [sum(x) for x in zip(state[-i], block)]
            break
    else:
        state[0] = [sum(x) for x in zip(state[0], block)]

    return state

def makeYokoBlock(block_data):
    block = [0, 0, 0, 0, 0]
    i = block_data[2]
    j = block_data[1]
    for x in xrange(j):
        block[i - 1 + x] = 1

    return block

def test():
    data = [[1,4,1],
            [1,3,1],
            [2,2,4],
            [2,3,5]]

    # print(makeYokoBlock([1,3,2]))
    # print(checkBlock([1, 1, 0, 0, 0], [0, 0, 1, 1, 0]))
    # state = [[1, 1, 1, 1, 0], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0]]
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = []
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = [[1, 1, 1, 1, 0]]
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = [[1, 1, 1, 1, 1], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]
    # print(clearTetoris(state))
    # print(makeTateBlocks([2, 3, 2]))
    # state = [[1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 1]]
    # print(tateBlock(state, [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]))
    # print(countRestBlock(state))
    # print(restBlock(data))

if __name__=='__main__':
    # test()
    main()

Submission status: Accepted

Solution code:
def yoko():
    for h, line in enumerate(L):
        if line[q-1:q-1+p] != [' ']*p:
            h = h-1
            for w in range(q-1, q-1+p):
                L[h][w] = '#'
            break
    else:
        for w in range(q-1, (q-1+p-1)+1):
            L[h][w] = '#'
    
    check(h, 1)
    
def tate():
    for h, line in enumerate(L):
        if line[q-1] != ' ':
            h = h-1
            for i in range(h-p+1, h-p+1+p):
                L[i][q-1] = '#'
            break
    else:
        for i in range(h-p+1, h-p+1+p):
            L[i][q-1] = '#'
    
    check(h-p+1, p)

def check(h, _h):
    c = 0
    while c < _h:
        if L[h] == ['#']*5:
            del L[h]
        else:
            h += 1
        c += 1
     
def add():
    c = 0
    for line in L:
        if line == [' ']*5:
            c += 1
        else:
            break
    for i in range(5-c):
        L.insert(0, [' ']*5)
        
def result():
    ans = 0
    for line in L:
        for v in line:
            if v == '#':
                ans += 1
    else:
        print ans

while True:
    n = input()
    if n == 0: break
    L = [[' ']*5 for i in range(5)]
    for i in range(n):
        d, p, q = map(int, raw_input().split())
        if d == 1:
            yoko()
        else:
            tate()
        add()
    else:
        result()

Submission status: Accepted

Solution code:
def yoko():
    for h, line in enumerate(L):
        if line[q-1:q-1+p] != [' ']*p:
            h = h-1
            break
    for w in range(q-1, (q-1+p-1)+1):
        L[h][w] = '#'
    check(h, 1)
    
def tate():
    for h, line in enumerate(L):
        if line[q-1] != ' ':
            h = h-1
            break
    for i in range(h-p+1, h-p+1+p):
        L[i][q-1] = '#'
    check(h-p+1, p)

def check(h, _h):
    c = 0
    while c < _h:
        if L[h] == ['#']*5:
            del L[h]
        else:
            h += 1
        c += 1
     
def add():
    c = 0
    for line in L:
        if line == [' ']*5:
            c += 1
        else:
            break
    for i in range(5-c):
        L.insert(0, [' ']*5)
        
def result():
    ans = 0
    for line in L:
        for v in line:
            if v == '#':
                ans += 1
    else:
        print ans

while True:
    n = input()
    if n == 0: break
    L = [[' ']*5 for i in range(5)]
    for i in range(n):
        d, p, q = map(int, raw_input().split())
        if d == 1:
            yoko()
        else:
            tate()
        add()
    else:
        result()

Submission status: Accepted

Solution code:
def yoko():
    for h, line in enumerate(L):
        if line[q-1:q-1+p] != [' ']*p:
            h = h-1
            break
    for i in range(q-1, (q-1+p-1)+1):
        L[h][i] = '#'
    check(h, 1)
    
def tate():
    for h, line in enumerate(L):
        if line[q-1] != ' ':
            h = h-1
            break
    for i in range(h-p+1, h-p+1+p):
        L[i][q-1] = '#'
    check(h-p+1, p)

def check(h, c):
    while 0 < c:
        if L[h] == ['#']*5:
            del L[h]
        else:
            h += 1
        c -= 1
     
def add():
    c = 0
    for line in L:
        if line == [' ']*5:
            c += 1
        else:
            break
    for i in range(5-c):
        L.insert(0, [' ']*5)
        
def result():
    ans = 0
    for line in L:
        for v in line:
            if v == '#':
                ans += 1
    else:
        print ans

while True:
    n = input()
    if n == 0: break
    L = [[' ']*5 for i in range(5)]
    for i in range(n):
        d, p, q = map(int, raw_input().split())
        if d == 1:
            yoko()
        else:
            tate()
        add()
    else:
        result()

Submission status: Accepted

Solution code:
def yoko():
    for h, line in enumerate(L):
        if line[q-1:q-1+p] != [' ']*p:
            h = h-1
            break
    for i in range(q-1, q-1+p):
        L[h][i] = '#'
    check(h, 1)
    
def tate():
    for h, line in enumerate(L):
        if line[q-1] != ' ':
            h = h-1
            break
    for i in range(h-p+1, h-p+1+p):
        L[i][q-1] = '#'
    check(h-p+1, p)

def check(h, c):
    while 0 < c:
        if L[h] == ['#']*5:
            del L[h]
        else:
            h += 1
        c -= 1
     
def add():
    c = 0
    for line in L:
        if line == [' ']*5:
            c += 1
        else:
            break
    for i in range(5-c):
        L.insert(0, [' ']*5)
        
def result():
    ans = 0
    for line in L:
        for v in line:
            if v == '#':
                ans += 1
    else:
        print ans

while True:
    n = input()
    if n == 0: break
    L = [[' ']*5 for i in range(5)]
    for i in range(n):
        d, p, q = map(int, raw_input().split())
        if d == 1:
            yoko()
        else:
            tate()
        add()
    else:
        result()

Submission status: Accepted

Solution code:
def v_block():
    block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1:q+p-1] != [' ']*p:
            break
    else:
        l += 1
    block[l-1][q-1:q+p-1] = ['*']*p
    return delete()


def h_block():
    for _p in range(p):
        block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1] == '*':
            break
    else:
        l += 1
    for _l in range(l-1, l-p-1, -1):
        block[_l][q-1] = '*'
    return delete()
                

def delete():
    l = 0
    while True:
        if block[l] == [' ']*5 or block[l] == ['*']*5:
            del block[l]
        else:
            l += 1
        if len(block) == l:
            return


def count():
    x = 0
    for line in block:
        for b in line:
            if b == '*':
                x += 1
    else:
        print(x)


while True:
    n = int(input())
    if n == 0:
        break
    block = [[' ']*5]
    for i in range(n):
        d, p, q = map(int, input().split())
        if d == 1:
            v_block()
        else:
            h_block()
    else:
        count()

Submission status: Accepted

Solution code:
def v_block():
    block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1:q+p-1] != [' ']*p:
            break
    else:
        l += 1
    block[l-1][q-1:q+p-1] = ['*']*p
    return delete()


def h_block():
    for _p in range(p):
        block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1] == '*':
            break
    else:
        l += 1
    for _l in range(l-1, l-p-1, -1):
        block[_l][q-1] = '*'
    return delete()
                

def delete():
    b = len(block)
    l = 0
    while 0 < b:
        if block[l] == [' ']*5 or block[l] == ['*']*5:
            del block[l]
        else:
            l += 1
        b -= 1


def count():
    x = 0
    for line in block:
        for b in line:
            if b == '*':
                x += 1
    else:
        print(x)


while True:
    n = int(input())
    if n == 0:
        break
    block = [[' ']*5]
    for i in range(n):
        d, p, q = map(int, input().split())
        if d == 1:
            v_block()
        else:
            h_block()
    else:
        count()

Submission status: Accepted

Solution code:
MAXN=5000
def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                if set(L[i-1])==set([True]):
                    del L[i-1]
                    L.insert(0,[False]*5)
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
            if set(L[i])==set([True]):
                del L[i]
                L.insert(0,[False]*5)
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                for j in range(i-lng,i):
                    if set(L[j])==set([True]):
                        del L[j]
                        L.insert(0,[False]*5)
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
            for j in range(i-lng+1,i+1):
                if set(L[j])==set([True]):
                    del L[j]
                    L.insert(0,[False]*5)

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h+2,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+1+1)
					break
		else:
			for li in range(h+2,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+p)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(len(field))])

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+2)
					break
		else:
			for li in range(h,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+p)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(h)])

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		h=0
		while 1:
			if field[h]==[1]*5:
				del field[h]
				top=[x-1 for x in top]
			elif field[h]==[0]*5:
				break
			else:
				h+=1
		for w in range(5):
			for h in range(top[w]-1,-2,-1):
				if field[h][w]==1 or h==-1:
					top[w]=h+1
					break
	print sum([sum(field[h]) for h in range(max(top))])

Submission status: Accepted

Solution code:
while 1:
    N = int(input())
    if N == 0:
        break
    L = 5000
    MP = [[0]*L for i in range(5)]
    cs = [L]*5
    us = []
    U = [0]*L
    for i in range(N):
        d, p, q = map(int, input().split()); q -= 1
        if d == 1:
            y = L
            for j in range(p):
                y = min(y, cs[q+j])
            y -= 1
            for j in range(p):
                MP[q+j][y] = 1
                cs[q+j] = y

            if all(MP[j][y] for j in range(5)):
                us.append(y)
        else:
            y = cs[q] - p
            for j in range(p):
                MP[q][y+j] = 1
            cs[q] = y
            for j in range(p):
                if all(MP[k][y+j] for k in range(5)):
                    us.append(y+j)
        if us:
            y0 = us[-1]
            for y in us:
                U[y] = 1
            for j in range(5):
                cur = y0
                for k in range(y0, cs[j]-1, -1):
                    if U[k]:
                        continue
                    MP[j][cur] = MP[j][k]
                    cur -= 1
                for k in range(cur, cs[j]-1, -1):
                    MP[j][k] = 0
                while cur < L and MP[j][cur] == 0:
                    cur += 1
                cs[j] = cur
            for y in us:
                U[y] = 0
            us = []
    ans = 0
    for j in range(5):
        for k in range(cs[j], L):
            ans += MP[j][k]
    print(ans)

Submission status: Accepted

Solution code:
def solve():
    from bisect import bisect_left
    from sys import stdin
    f_i = stdin
    
    while True:
        n = int(f_i.readline())
        if n == 0:
            break
        
        b = [[0] for i in range(5)]
        for i in range(n):
            d, p, q = map(int, f_i.readline().split())
            q -= 1
            if d == 1:
                if p == 5:
                    continue
                
                h = 0
                for c in b[q:q+p]:
                    if c[-1] > h:
                        h = c[-1]
                
                h += 1
                for c in b[:q] + b[q+p:]:
                    if h not in c:
                        for c in b[q:q+p]:
                            c.append(h)
                        break
                else:
                    for c in b[:q] + b[q+p:]:
                        idx = bisect_left(c, h)
                        c.pop(idx)
                        for j in range(idx, len(c)):
                            c[j] -= 1
            else:
                h = b[q][-1] + 1
                b[q] += list(range(h, h + p))
                diff = 0
                for x in range(h, h + p):
                    x += diff
                    for c in b[:q] + b[q+1:]:
                        if x not in c:
                            break
                    else:
                        for c in b:
                            idx = bisect_left(c, x)
                            c.pop(idx)
                            for j in range(idx, len(c)):
                                c[j] -= 1
                        diff -= 1
        
        print(sum(map(len, b)) - 5)

solve()