Skip to content

Instantly share code, notes, and snippets.

@yihangho
Created July 19, 2014 05:06
Show Gist options
  • Save yihangho/a991ccf5b41986c4bd39 to your computer and use it in GitHub Desktop.
Save yihangho/a991ccf5b41986c4bd39 to your computer and use it in GitHub Desktop.
def successor((x,y),X,Y):
return set([
(0,y), # empty X
(x,0), # empty Y
(X,y), # fill X
(x,Y), # fill Y
(0,y+x) if x+y<=Y else (x-(Y-y), y+(Y-y)), # X->Y
(x+y,0) if x+y<=X else (x+(X-x), y-(X-x)), # Y->X
]) - set([(x,y)]) # remove self
def pour_problem(goal,X,Y):
start = (0,0)
frontier = [[start]]
explored = set([start])
while frontier:
path = frontier.pop(0)
u = path[-1]
for v in successor(u,X,Y):
if v not in explored:
explored.add(v)
new_path = path + [v]
if goal in v:
return new_path
else:
frontier.append(new_path)
return []
for goal in range(1,10):
print goal, pour_problem(goal,5,9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment