京东笔试题——消消乐问题

消消乐

# 1
a = [[3,1,2,1,1],[1,1,1,1,3],[1,1,1,1,1],[1,1,1,1,1],[3,1,2,2,2]]    


def candy(line):
    R, C = 5, 5
    changed = True
    while changed:
        changed = False
        for r in range(R):
            for c in range(C-2):
                if abs(line[r][c]) == abs(line[r][c+1]) == abs(line[r][c+2]) != 0:
                    line[r][c] = line[r][c+1] = line[r][c+2] = -abs(line[r][c])
                    changed = True
                    
        for r in range(R-2):
            for c in range(C):
                if abs(line[r][c]) == abs(line[r+1][c]) == abs(line[r+2][c]) != 0:
                    line[r][c] = line[r+1][c] = line[r+2][c] = -abs(line[r][c])
                    changed = True
        for c in range(C):
            i = R-1
            for r in reversed(range(R)):
                if line[r][c] > 0:
                    line[i][c] = line[r][c]
                    i -= 1
            for r in reversed(range(i+1)):
                line[r][c] = 0
    print(line)
    count = 0
    for i in range(5):
        for j in range(5):
            if line[i][j] != 0:
            	count += 1
    return count
        

print(candy(a))

 

你可能感兴趣的:(Python,笔试面经)