Problem:


Chain Disappearance Puzzle

We are playing a puzzle.
An upright board with H rows by 5 columns of cells, as shown in 
the figure below, is used in this puzzle.
A stone engraved with a digit, one of 1 through 9, is placed in each of 
the cells.
When three or more stones in horizontally adjacent cells are engraved 
with the same digit, the stones will disappear.
If there are stones in the cells above the cell with a disappeared 
stone, the stones in the above cells will drop down, filling the 
vacancy.

	

The puzzle proceeds taking the following steps.

	
When three or more stones in horizontally adjacent cells are 
engraved with the same digit, the stones will disappear. Disappearances 
of all such groups of stones take place simultaneously.

	
When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 

	
After the completion of all stone drops, if one or more groups of 
stones satisfy the disappearance condition, repeat by returning to the 
step 1.

The score of this puzzle is the sum of the digits on the disappeared stones.

Write a program that calculates the score of given configurations of stones.

	

Input

The input consists of multiple datasets.
Each dataset is formed as follows.

Board height H

Stone placement of the row 1

Stone placement of the row 2

...

Stone placement of the row H

The first line specifies the height ( H  ) of the puzzle board (1 &#8804; H &#8804; 10).
The remaining H lines give placement of stones on each of the rows from top to bottom.
The placements are given by five digits (1 through 9), separated by a space.
These digits are engraved on the five stones in the corresponding row, in the same order.

The input ends with a line with a single zero.

Output

For each dataset, output the score in a line.
Output lines may not include any characters except the digits expressing the scores.

Sample Input

1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0

Output for the Sample Input

36
38
99
0
72



Sample input:
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0


Sample output:
36
38
99
0
72

Submission status: Accepted

Solution code:
class Game:
    def __init__(self, mp, height):
        self.mp = mp
        self.height = height
        self.score = 0
        self.erase_flag = False

    def erase(self):
        for y in range(self.height):
            line = self.mp[y]
            new_line, add_score = self.fix(line)
            self.mp[y] = new_line
            self.score += add_score
            if add_score != 0:self.erase_flag = True

    def fix(self, line):
        add_score = 0
        new_line = line
        for length in range(5, 2, -1):
            for left in range(5 - length + 1):
                if all(map(lambda x:x==line[left], line[left:left+length])):
                    add_score = line[left] * length
                    new_line = line[:left] + [0] * length + line[left+length:]
                    return new_line, add_score
        return new_line, add_score

    def fall(self):
        for x in range(5):
            col = []
            for y in range(self.height):
                if self.mp[y][x] != 0:
                    col.append(self.mp[y][x])
            col = [0] * (self.height - len(col)) + col
            for y in range(self.height):
                self.mp[y][x] = col[y]

    def run(self):
        while True:
            self.erase_flag = False
            self.erase()
            if not self.erase_flag:break
            self.fall()

    def print_state(self):
        print(*self.mp, sep="\n")
        print(self.score)


while True:
    h = int(input())
    if h == 0:break
    mp = [list(map(int, input().split())) for _ in range(h)]
    game = Game(mp, h)
    game.run()
    print(game.score)

Submission status: Accepted

Solution code:
# coding: utf-8
while 1:
    h=int(input())
    if h==0:
        break
    field=[]
    for i in range(h):
        field.insert(0,list(map(int,input().split())))
        field[0]+=[0]
    ans=0
    f=True
    while f:
        f=False
        for i in range(h):
            now=-1
            count=0
            for j in range(6):
                if now!=field[i][j] and count>=3:
                    for k in range(count):
                        field[i][j-1-k]=0
                    ans+=now*count
                    count=0
                    f=True if now!=0 else f
                elif now!=field[i][j]:
                    count=0
                now=field[i][j]
                count+=1
        for i in range(h-1):
            for j in range(5):
                if field[i][j]==0:
                    for k in range(i+1,h):
                        if field[k][j]!=0:
                            field[i][j],field[k][j]=field[k][j],field[i][j]
                            break
    print(ans)

Submission status: Runtime Error

Solution code:
def calc(pp,x,y):
    p = 0
    for i in range(1,1000):
        if i*(100+x)/100 == pp:
            p = i
            break
    return p*(100+y)/100

while True:
    max = 0
    x,y,s = map(int,raw_input().split())
    if x == 0 and y == 0 and s == 0:
        break
    for i in range(1,s):
        res = calc(i,x,y) + calc(s-i,x,y)
        if max < res:
            max = res
    print max

Submission status: Runtime Error

Solution code:
while True:
  lst=[]
  pt=0
  H=int(input()) 
  for i in range(H):
   lst.append(list(map(int,input().split())))
  for s in lst:
    for s2 in range(0,3):
     if s[s2] != s and s[s2] == s[s2+1] == s[s2+2]:
       x=s[s2]
       pt += s[s2]*s.count(x)
       for h in range(0,s.count(x)):
         s[s.index(x)]=0
       for g in lst:
         for f in range(5):
           try:
             if g[f]==0:
                 lst.reverse()
                 g[f]=lst[lst.index(g)-1][f]
                 ls[ls.index(g)-1]
             else:
               pass
           except:
             break
  print(pt)