Guest User

Untitled

a guest
Feb 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. require_relative "q50disp"
  2.  
  3. W, H = 6, 5
  4.  
  5. def putout
  6. return if @memo.include?([@yoko, @tate])
  7. @memo << (a = Marshal.load(Marshal.dump([@yoko, @tate])))
  8. Disp.show(*a)
  9. end
  10.  
  11. def yoko?(y)
  12. @yoko[y].inject(:+) < 2
  13. end
  14.  
  15. def tate?(x)
  16. @tate[x].inject(:+) < 2
  17. end
  18.  
  19. def search(x, y, co)
  20. if x == W and y == H
  21. putout if co >= 25
  22. return
  23. end
  24. if x < W and yoko?(y)
  25. @yoko[y][x] += 1
  26. search(x + 1, y, co + 1)
  27. @yoko[y][x] -= 1
  28. end
  29. if y < H and tate?(x)
  30. @tate[x][y] += 1
  31. search(x, y + 1, co + 1)
  32. @tate[x][y] -= 1
  33. end
  34. if x > 0 and yoko?(y)
  35. @yoko[y][x - 1] += 1
  36. search(x - 1, y, co + 1)
  37. @yoko[y][x - 1] -= 1
  38. end
  39. if y > 0 and tate?(x)
  40. @tate[x][y - 1] += 1
  41. search(x, y - 1, co + 1)
  42. @tate[x][y - 1] -= 1
  43. end
  44. end
  45.  
  46. @tate = Array.new(W + 1) {Array.new(H, 0)}
  47. @yoko = Array.new(H + 1) {Array.new(W, 0)}
  48. @memo = []
  49.  
  50. search(0, 0, 0)
Add Comment
Please, Sign In to add comment