fork download
  1. E=enumerate
  2. def G(g):
  3. while g:
  4. q,s=[(t:=[*g][0])],[t];g-={t}
  5. for x,y in q:
  6. for u in[(x+1,y),(x,y+1),(x,y-1),(x-1,y)]:
  7. if u in g:s+=u,;q+=u,;g-={u}
  8. yield{*s}
  9. def f(b):
  10. B=[[0]*len(i)for i in b]
  11. for i in G({(x,y)for x,r in E(b)for y,v in E(r)if v}):
  12. for j in i:
  13. if~-len([*G(i-{j})]):x,y=j;B[x][y]=1
  14. return B
  15.  
  16. def to_board(s):
  17. return [[*map(int, i)]for i in filter(None, s.split('\n'))]
  18.  
  19. s1 = """
  20. 11101001
  21. 11011101
  22. 00000001
  23. 11101111
  24. 11110101
  25. 00011111
  26. 10110001
  27. 11111111
  28. """
  29. s1_g = """
  30. 01000000
  31. 00001001
  32. 00000001
  33. 00000101
  34. 00110000
  35. 00010000
  36. 00000000
  37. 11100000
  38. """
  39. s2 = """
  40. 1111111111111111
  41. 1000000000000001
  42. 1111111111111101
  43. 0000000000000101
  44. 1111111111110101
  45. 1000000000010101
  46. 1011111111010101
  47. 1010000001010101
  48. 1010111101010101
  49. 1010101111010101
  50. 1010100000010101
  51. 1010111111110101
  52. 1010000000000101
  53. 1011111111111101
  54. 1000000000000001
  55. 1111111111111111
  56. """
  57. s2_g = """
  58. 0000000000000000
  59. 0000000000000000
  60. 0000000000000000
  61. 0000000000000000
  62. 0000000000000000
  63. 0000000000000000
  64. 0000000000000000
  65. 0000000000000000
  66. 0000000000000000
  67. 0000000000000000
  68. 0000000000000000
  69. 0000000000000000
  70. 0000000000000000
  71. 0000000000000000
  72. 0000000000000000
  73. 0000000000000000
  74. """
  75. s3 = """
  76. 1011010001111010
  77. 1111111011101101
  78. 1110010101001011
  79. 1111001110010010
  80. 1111010000101001
  81. 0111101001000101
  82. 0011100111110010
  83. 1001110011111110
  84. 0101000011100011
  85. 1110110101001110
  86. 0010100111000110
  87. 1000110111011010
  88. 0100101000100101
  89. 0001010101100011
  90. 1001010000111101
  91. 1000111011000010
  92. """
  93. s3_g = """
  94. 0000000000111010
  95. 1011110001001000
  96. 0000000000000011
  97. 0000000100010000
  98. 0000010000101000
  99. 0000001000000100
  100. 0000000011000000
  101. 1001100000011110
  102. 0000000001000010
  103. 0110100001000110
  104. 0000100101000010
  105. 1000100000000000
  106. 0100001000000100
  107. 0000000100100001
  108. 0000010000111000
  109. 0000010000000010
  110. """
  111. print(f(to_board(s1))==to_board(s1_g))
  112. print(f(to_board(s2))==to_board(s2_g))
  113. print(f(to_board(s3))==to_board(s3_g))
Success #stdin #stdout 0.08s 14172KB
stdin
Standard input is empty
stdout
True
True
True