fork download
  1. import copy
  2.  
  3. """ スペースを整える(宙に浮いたぷよを落とす) """
  4. def fix_space( a ):
  5. for y in range( len(a)-1, 0, -1 ):
  6. for x in range( len(a[y]) ):
  7. if a[y][x] == '.':
  8. for z in range( y-1, -1, -1 ):
  9. if a[z][x] != '.':
  10. a[y][x], a[z][x] = a[z][x], a[y][x]
  11. break
  12.  
  13. """ 位置 (x,y) のぷよを起点に、c と同じ色の隣接するぷよの数を計算する """
  14. def count_matched_puyo( a, x, y, c ):
  15. if not (0 <= y < len(a)) or not (0 <= x < len(a[y])) or a[y][x] != c:
  16. return 0
  17. else:
  18. a[y][x] = '.'
  19. return 1 + \
  20. count_matched_puyo( a, x-1, y, c ) + \
  21. count_matched_puyo( a, x+1, y, c ) + \
  22. count_matched_puyo( a, x, y-1, c ) + \
  23. count_matched_puyo( a, x, y+1, c )
  24.  
  25. """ マッチするぷよを消す、消されたぷよがあれば真を返す """
  26. def erase_matched_puyo( a ):
  27. fix_space(a)
  28. f, s = False, copy.deepcopy(a)
  29. for y in range( len(a) ):
  30. for x in range( len(a[y]) ):
  31. c = a[y][x]
  32. if c.isdigit() and count_matched_puyo( copy.deepcopy(s), x, y, c ) >= 4:
  33. f, a[y][x] = True, '.'
  34. return f
  35.  
  36. """ 連鎖数を計算する """
  37. def get_num_of_chain( a ):
  38. n = 0
  39. while erase_matched_puyo(a) == True:
  40. n += 1
  41. return n
  42.  
  43. """ 問題を解く """
  44. def solve( lines ):
  45. a = list( list(line) for line in lines )
  46. for line in a:
  47. print( ''.join(line) )
  48. print( '=> {}'.format( get_num_of_chain(a) ) )
  49.  
  50. solve( [ '0013', '0123', '122.', '013.', '0123' ] )
  51. solve( [ '9.9.9.9.9.9', '.9.9.9.9.9.' ] )
  52. solve( [ '0123', '3012', '2301', '1230' ] )
  53.  
Success #stdin #stdout 0.02s 27688KB
stdin
Standard input is empty
stdout
0013
0123
122.
013.
0123
=> 4
9.9.9.9.9.9
.9.9.9.9.9.
=> 1
0123
3012
2301
1230
=> 0