Skip to content

Instantly share code, notes, and snippets.

@takuma-saito
Created February 3, 2019 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuma-saito/0f765924774d9373456dadb11d7dfd79 to your computer and use it in GitHub Desktop.
Save takuma-saito/0f765924774d9373456dadb11d7dfd79 to your computer and use it in GitHub Desktop.
q-puzzle.rb
# 9 パズルビームサーチの擬似コード
queue.push({s: {7, 8, 0, 1, 2, 3, 4, 5, 6}, r: 2})
while (not queue.empty())
pqueue = PriorityQueue.new
while (q = queue.pop())
[[-1, 0], [0, 1], [1, 0], [0, -1]].each do |d|
newState = state(q, d)
pqueue.push(newState)
pqueue.pop() if pqueue.size > K
end
end
while (q = pqueue.pop())
queue.push(q) # 逆順にしておく
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment