Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
  2.  
  3. with open('mooyomooyo.in', 'r') as file:
  4. n, k = map(int, file.readline().split())
  5. board = []
  6. for i in range(n):
  7. row = file.readline().strip()
  8. board.append([int(j) for j in row])
  9.  
  10. v = len(board[0]) # v is the variable for columns
  11.  
  12. current_tiles = 0
  13.  
  14. def floodfill(row, col):
  15. global current_tiles
  16. current_tiles += 1
  17. value = board[row][col]
  18. visited[row][col] = True
  19. if current_tiles >= k:
  20. board[row][col] = 0
  21. for direction in directions:
  22. nrow, ncol = row+direction[0], col+direction[1] # new row, new col
  23. if nrow not in range(n):
  24. continue
  25. if ncol not in range(v):
  26. continue
  27. if value != board[nrow][ncol]:
  28. continue
  29. if visited[nrow][ncol] and current_tiles < k:
  30. continue
  31. floodfill(nrow, ncol)
  32.  
  33. def simulate_gravity():
  34. for j in range(v):
  35. column = [board[i][j] for i in range(n) if board[i][j] != 0]
  36. column = [0 for i in range(n-len(column))] + column
  37. for i in range(n):
  38. board[i][j] = column[i]
  39.  
  40. while True:
  41. deletion_occurred = False
  42. for i in range(n):
  43. for j in range(v):
  44. if board[i][j] != 0:
  45. current_tiles = 0
  46. visited = [[False for j in range(v)] for i in range(n)]
  47. floodfill(i, j)
  48. if current_tiles >= k:
  49. deletion_occurred = True
  50. if deletion_occurred:
  51. simulate_gravity()
  52. else:
  53. break
  54.  
  55. with open('mooyomooyo.out', 'w') as file:
  56. for row in board:
  57. print(*row, sep='', file=file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement