fork download
  1. from math import*
  2. L=len
  3. p={'A':(k:=[3,2,1],1),'H':([5]*3,1),'B':(k,0)}
  4. g=lambda x,i:L(set([j[i]for j in x]))
  5. def u(b,x,y,l,r,o,Q,i=0,H=0):
  6. j=b[x][y];P=max(abs(o[0][0]-x),abs(o[0][1]-y))-1
  7. if i*~-(j in p)and int(j)<l[P]:b[x][y]=l[P]
  8. if(j=='B')*i*(P<3):Q.append((x,y))
  9. for Z in[(x,y+1),(x+1,y),(x,y-1),(x-1,y)]:
  10. if Z[0]>=(Z in o)==0<=Z[1]and(r or 1in[g(O:=o+[Z],0),g(O,1)]):
  11. try:
  12. if(K:=dist(o[0],Z))>=H:u(b,Z[0],Z[1],l,r,o+[Z],Q,1,K)
  13. except:1
  14. def f(b):
  15. Q,S=[],[];Z=L(b[0])
  16. for x in range(L(b)*Z):
  17. if str(E:=b[x//Z][x%Z])in'AH':u(b,x//Z,x%Z,*p[E],[(x//Z,x%Z)],Q)
  18. while Q:
  19. S+=Q;q=[]
  20. for i in Q:u(b,*i,*p['B'],[i],q)
  21. Q=[j for j in set(q)-set(S)]
  22.  
  23. tests = [["""H""", 'H'],
  24. ["""000000000000000
  25. A00A0000000000A
  26. 000000000000000
  27. 000000000000000
  28. 000000000000000
  29. 000000000000000
  30. 000000000000000
  31. 000000000000000
  32. 0000000A0000000
  33. 000000000000000
  34. 000000000000000
  35. 000000000000000
  36. 000000000000000
  37. 000000000000000
  38. A000000000000AA""","""333332100001233
  39. A33A3210000123A
  40. 333332100001233
  41. 222222100001222
  42. 111111100001111
  43. 000011111110000
  44. 000012222210000
  45. 000012333210000
  46. 0000123A3210000
  47. 000012333210000
  48. 000012222210000
  49. 111111111111111
  50. 222100000012222
  51. 332100000012333
  52. A321000000123AA"""],
  53. ["""000000000000000000
  54. 0AAAA00000000AAAA0
  55. 000000000000000000
  56. 000000000000000000
  57. 000000000000000000
  58. 000000000000000000
  59. AA0000000000000000
  60. 000000000000000000
  61. 000000000000000000
  62. 000000000000000000
  63. 00000000000000A000
  64. 000000000000000000
  65. 000000000000000000
  66. 00000A000000000000
  67. 000000000000000000
  68. 000000000000000000
  69. 00000000000000000A
  70. A00A000A0000A00000""","""333333210012333333
  71. 3AAAA32100123AAAA3
  72. 333333210012333333
  73. 222222210012222222
  74. 222211110011111111
  75. 333210000000000000
  76. AA3210000000000000
  77. 333210000001111111
  78. 222210000001222221
  79. 111110000001233321
  80. 00111111100123A321
  81. 001222221001233321
  82. 001233321001222221
  83. 00123A321001111111
  84. 111233321111111222
  85. 222222222222222233
  86. 33333233322333223A
  87. A33A323A3223A32233"""],
  88. ["""H00000000000000000
  89. 000000000000000H00
  90. 000000000000000000
  91. 00000000H000000000
  92. 000000000000000000
  93. 000000000000000000
  94. 000000000000000000
  95. 000000HH0000000000
  96. 000000000000000000
  97. 000000000000000000
  98. 000000000000000H00
  99. 000000000000000000
  100. 000000000000000000
  101. 000000000000000000
  102. HHH00000000H000000
  103. 000000000000000000
  104. 000000000000000000
  105. 000000000000000000""","""H55505555555555555
  106. 555505555555555H55
  107. 555505555555555555
  108. 55550555H555555555
  109. 000555555555555555
  110. 000555555555000000
  111. 000555555555000000
  112. 000555HH5550555555
  113. 000555555550555555
  114. 000555555550555555
  115. 000555555550555H55
  116. 555555005555555555
  117. 555555005555555555
  118. 555555005555555555
  119. HHH55500555H555000
  120. 555555005555555000
  121. 555555005555555000
  122. 555555005555555000"""],
  123. ["""000000000000000000000A
  124. 00000B0000000000000000
  125. 0000000000000000000000
  126. 0B0000000000000000000B
  127. 000000000000000000B000
  128. 0000000000000000000000
  129. 000000000000000000000B
  130. 0000000000000000000000
  131. 000000B00000000A000000
  132. 000000000000000000000B
  133. 0000000000000000000000
  134. 000000B000000000000000
  135. 000000000000000000000B
  136. 0000000000000000000000
  137. 000000000H000000000000
  138. 000000000000000000000B
  139. 0000000000000000000000
  140. A000000000000000000000
  141. A00000000000B000000000
  142. A000000000000000000000
  143. HHHH00000000000000A00H
  144. 00000000A0000000000000""","""000000000000000000123A
  145. 00000B0000000000001233
  146. 0000000000000000001223
  147. 0B0000000000000000123B
  148. 000000000000000000B003
  149. 0000001000001111111003
  150. 000000200000122222123B
  151. 0000003000001233321003
  152. 000123B32100123A321003
  153. 000000300000123332123B
  154. 0000003000001222221003
  155. 000123B555555111111003
  156. 000000555555500000123B
  157. 0000005555555000000003
  158. 111100555H555000000003
  159. 222100555555500000123B
  160. 3321005555555000000003
  161. A555555555555001115555
  162. A55555511111B001225555
  163. A555555222210001235555
  164. HHHH55533321000123A55H
  165. 55555553A3210001235555"""],["""0000000000
  166. 0A00000000
  167. 0000000000
  168. 000B00B000
  169. 0000000000
  170. 000B00B00B
  171. 0000000000
  172. 0B00B00000
  173. 0000000000
  174. 0000000000""","""3332101000
  175. 3A32102000
  176. 3333103001
  177. 223B33B322
  178. 1113103003
  179. 123B33B33B
  180. 0003003003
  181. 0B02B02002
  182. 0001001001
  183. 0000000000"""]]
  184. def to_board(d):
  185. return [list(i)for i in filter(None,d.split('\n'))]
  186.  
  187. for a, b in tests:
  188. board = to_board(a)
  189. f(board)
  190. assert (r1:='\n'.join(''.join(map(str,i))for i in board)) == b
Success #stdin #stdout 0.15s 14076KB
stdin
Standard input is empty
stdout
Standard output is empty