京东消消乐
线下跑几个例子暂时没发现问题...记录一下
#消消乐 def gravity(grid,visited): # print('before:',grid) for col in range(5): cnt=0 for row in range(4,-1,-1): if grid[row][col]=='x': cnt+=1 elif cnt>0: grid[row+cnt][col]=grid[row][col] grid[row][col]='x' visited[row][col],visited[row+cnt][col]=visited[row+cnt][col],visited[row][col] return grid def judge(grid,i,j): if i<3 and grid[i+1][j]==grid[i][j] and grid[i+2][j]==grid[i][j]: return True if j<3 and grid[i][j+1]==grid[i][j] and grid[i][j+2]==grid[i][j]: return True if i<4 and j<4 and grid[i+1][j]==grid[i][j] and grid[i][j+1]==grid[i][j]: return True if i<4 and j<4 and grid[i+1][j]==grid[i][j] and grid[i+1][j+1]==grid[i][j]: return True if i>=1 and j>0 and grid[i][j-1]==grid[i][j] and grid[i-1][j]==grid[i][j]: return True return False while 1: try: grid=[] for row in range(5): line=list(map(int,input().split())) grid.append(line) visited=[[0 for _ in range(5)]for _ in range(5)] direction=[[0,1],[1,0],[-1,0],[0,-1]] for k in range(5): for g in range(5): que = [[k, g]] num = grid[k][g] if num=='x' or visited[k][g] or not judge(grid,k,g): continue else: flag=0 while que: i,j=que.pop(0) for x,y in direction: #满足两个以上则可以消除 if i+x>=0 and j+y>=0 and i+x<5 and j+y<5 and not visited[i+x][j+y] and grid[i+x][j+y]==num: visited[i+x][j+y]=1 grid[i+x][j+y]='x' flag+=1 que.append([i+x,j+y]) if flag>0: visited[k][g]=1 grid[k][g]='x' grid=gravity(grid,visited) print(grid) cnt=25 for i in range(5): cnt-=grid[i].count('x') print(cnt) except: break