fork download
  1. E=enumerate
  2. def f(s,b):
  3. d={(x,y):v for x,r in E(b)for y,v in E(r)}
  4. for i in d:
  5. q=[(*i,s[1:],[i])]
  6. if s[0]==d[i]:
  7. while q:
  8. (x,y,S,p),*q=sorted(q,key=lambda x:len(x[2]))
  9. if''==S:return 1
  10. q+=[(*v,S[1:],p+[v])for v in[(x+1,y),(x-1,y),(x,y+1),(x,y-1)]if(d.get(v)==S[0])>(v in p)]
  11.  
  12. s = """
  13. 010101
  14. 111011
  15. 011010
  16. 011011
  17. """
  18. s1 = """
  19. 01010
  20. 10101
  21. 01010
  22. 10101
  23. 01010
  24. """
  25. s2 = """
  26. 01110
  27. 01100
  28. 10010
  29. 10110
  30. 01101
  31. """
  32. s3 = """
  33. 0
  34. """
  35. s4 = """
  36. 10
  37. 01
  38. """
  39. s5 = """
  40. 100
  41. 010
  42. 001
  43. """
  44.  
  45. s6 = """
  46. 00000
  47. 00000
  48. 00000
  49. 00000
  50. 00000
  51. """
  52.  
  53. s7 = """
  54. 10101
  55. 01010
  56. 10101
  57. 01010
  58. 10101
  59. """
  60.  
  61. s8 = """
  62. 100
  63. 010
  64. 001
  65. """
  66.  
  67. s9 = """
  68. 10001
  69. 01010
  70. 00100
  71. 01010
  72. 10001
  73. """
  74. def to_board(s):
  75. return [[*i] for i in filter(None, s.split('\n'))]
  76.  
  77. print(f('0111111100101', to_board(s)))
  78. print(f('0101010101010101010101010', to_board(s1)))
  79. print(f('011111000110100', to_board(s2)))
  80. print(f('0', to_board(s3)))
  81. print(f('1010', to_board(s4)))
  82. print(f('100010001', to_board(s5)))
  83. print('-'*50)
  84. print(f('1', to_board(s6)))
  85. print(f('11', to_board(s7)))
  86. print(f('111', to_board(s8)))
  87. print(f('1000100010001000101010100', to_board(s9)))
Success #stdin #stdout 0.67s 14148KB
stdin
Standard input is empty
stdout
1
1
1
1
1
1
--------------------------------------------------
None
None
None
None