fork download
  1. from itertools import*
  2. M=[(1,0),(-1,0),(0,-1),(0,1)]
  3. N=int
  4. E=enumerate
  5. def f(b):
  6. Z=eval(str(b));q=[(d:={(x,y):v for x,r in E(b)for y,v in E(r)},{i:0 for i in d if'/'<d[i]<':'})]
  7. for a,b in q:
  8. if[]==(k:=[j for j in b if b[j]!=N(a[j])]):
  9. for(x,y),v in a.items():Z[x][y]=v
  10. return'\n'.join(map(''.join,Z))
  11. t=min(k,key=lambda x:N(a[x]));x,y=t;Q,S=[(N(a[t])-b[t],4,[])],[];W=[]
  12. for X,e,O in Q:
  13. if 1>X:S+=O,;continue
  14. for i in[1,2]:
  15. if X//i*e:Q+=(X-i,e-1,O+[i]),
  16. for O in S:
  17. for D in combinations(M,len(O)):
  18. A,B,F=[],[],1
  19. for o,(X,Y)in zip(O,D):
  20. j,k=x,y;F=1
  21. while F:
  22. if(T:=(j+X,k+Y))not in a:F=0
  23. elif'/'<a[T]<':':
  24. if~N(a[T])+b[T]<o:B+=(T,o),
  25. else:F=0
  26. break
  27. elif' '!=a[T]:F=0
  28. else:j,k=T;A+=(T,'|║-═'[o//2+2*(X==0)]),
  29. if F:W+=({**a,**{T:R for T,R in A}},{**b,**{T:b[T]+R for T,R in B},(x,y):N(a[(x,y)])}),
  30. for i in W:q+=i,
  31.  
  32. s1 = """
  33. 2 2 1
  34. 1 4 3
  35. 3 2
  36. 4
  37. 3 2 3
  38. 1
  39. 3 4 2
  40. """
  41. s2 = """
  42. 2 2 2 2 1 1 2 2 2 2 2
  43. 1 3 5 4 4 2
  44. 2 2 4 5 5 4 2 2 1 3
  45. 2 1 1 3 3 2 2
  46. 3 4 4 4 4 5 4 3 2 3
  47. 2 4 5 4 2 3
  48. 2 1 4 2 4 3 1 1 2
  49. 2 1 3 1 1 6 4 2
  50. 3 2 4 3 6 3 2
  51. 2 2 3 3 2 5 2 4 3
  52. 2 1 1 2
  53. 1 3 3 3 3 5 8 7 6 5 4
  54. 2 3 1 1 2
  55. 1 1 5 1 4 5 6 3 1 2
  56. 1 1 2 2 3 4
  57. 3 5 4 4 3 3 8 7 5 1 2
  58. 2 3 1 2 2 1 1
  59. 2 2 2 2 5 7 6 3 3
  60. 3 3 6 3 5 3 2 2 2 3
  61. 2 1 2 3 2 2
  62. 3 4 6 4 5 5 3 3 5 1
  63. 2 1 2 2 1 1 3
  64. 2 1 1 2 3 6 5 2 2
  65. 2 3 4 4 4 2 1
  66. 2 2 2 2 2 2 2 1 1 3 2
  67. """
  68. def to_board(s):
  69. return [[*i]for i in filter(None, s.split('\n'))]
  70.  
  71. print(f(to_board(s1)))
Success #stdin #stdout 2.26s 309700KB
stdin
Standard input is empty
stdout
2-2--1 
|1--4-3
3--2║ ║
|  |4 ║
|3-2║ 3
1║  ║ |
 3--4-2