Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_relative "q50disp"
- W, H = 6, 5
- def putout
- return if @memo.include?([@yoko, @tate])
- @memo << (a = Marshal.load(Marshal.dump([@yoko, @tate])))
- Disp.show(*a)
- end
- def yoko?(y)
- @yoko[y].inject(:+) < 2
- end
- def tate?(x)
- @tate[x].inject(:+) < 2
- end
- def search(x, y, co)
- if x == W and y == H
- putout if co >= 25
- return
- end
- if x < W and yoko?(y)
- @yoko[y][x] += 1
- search(x + 1, y, co + 1)
- @yoko[y][x] -= 1
- end
- if y < H and tate?(x)
- @tate[x][y] += 1
- search(x, y + 1, co + 1)
- @tate[x][y] -= 1
- end
- if x > 0 and yoko?(y)
- @yoko[y][x - 1] += 1
- search(x - 1, y, co + 1)
- @yoko[y][x - 1] -= 1
- end
- if y > 0 and tate?(x)
- @tate[x][y - 1] += 1
- search(x, y - 1, co + 1)
- @tate[x][y - 1] -= 1
- end
- end
- @tate = Array.new(W + 1) {Array.new(H, 0)}
- @yoko = Array.new(H + 1) {Array.new(W, 0)}
- @memo = []
- search(0, 0, 0)
Add Comment
Please, Sign In to add comment