Skip to content

Instantly share code, notes, and snippets.

@obelisk68
Last active December 1, 2017 04:09
Show Gist options
  • Save obelisk68/b40fada90b0619bba120435c91947695 to your computer and use it in GitHub Desktop.
Save obelisk68/b40fada90b0619bba120435c91947695 to your computer and use it in GitHub Desktop.
2つのバケツ
#http://obelisk.hatenablog.com/entry/2017/12/01/041951
#B1リットル: 空(0), 入れる(1), B1→B2に移す(2)
#B2リットル: 空(3), 入れる(4), B2→B1に移す(5)
B1, B2 = 8, 5
TO = 1
def move(mass, f, t)
if (a = mass - t) >= f
t += f
f = 0
else
t = mass
f -= a
end
[f, t]
end
procedures = [[[1], 0, 0, [[0, 0]]], [[4], 0, 0, [[0, 0]]]]
while (pr = procedures.shift)
case pr[0][-1]
when 1 then pr[1] = B1
when 4 then pr[2] = B2
when 0 then pr[1] = 0
when 3 then pr[2] = 0
when 2
pr[1], pr[2] = move(B2, pr[1], pr[2])
when 5
pr[2], pr[1] = move(B1, pr[2], pr[1])
end
if pr[1] == TO or pr[2] == TO
p pr[0]
next
end
next if pr[3].include?(a = [pr[1], pr[2]])
pr[3] << a
next if pr[1] == B1 and pr[2] == B2
6.times do |i|
a = pr[0].dup
a << i
procedures.push([a, pr[1], pr[2], pr[3]])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment