
def water_helper(x,y,z,next,moves,all_moves):
    if next in moves: return
    new_moves = moves[:]
    new_moves.append(next)
    water(x,y,z,next,new_moves,all_moves)

def water(x, y, z, cur, moves, all_moves):
    ''' Given buckets with capacities x L and y L, x > y, find all 
    sequences of moves that end with the x L bucket containing z L of water. 
    Store the "current" sequence of moves being investigated in the
    moves argument, and store all the valid sequence of moves in 
    all_moves. 

    '''
    if cur[0] == z: all_moves.append(moves)

    # dump the larger bucket
    if cur[0] > 0:
        next = (0,cur[1])          
        water_helper(x,y,z,next,moves,all_moves)
    
    # dump the smaller bucket 
    if cur[1] > 0:
        next = (cur[0],0)          
        water_helper(x,y,z,next,moves,all_moves)
    
    # fill the larger bucket 
    if cur[0] < x: 
        next = (x,cur[1])          
        water_helper(x,y,z, next, moves, all_moves)
    
    # fill the smaller bucket
    if cur[1] < y:
        next = (cur[0], y)         
        water_helper(x,y,z,next, moves, all_moves)
        
    # transfer from larger to smaller bucket
    if cur[0] > 0 and cur[1] < y:  
        next = (max(0,cur[0]-(y-cur[1])), min(y, cur[1] + cur[0]))
        water_helper(x,y,z,next,moves, all_moves)
        
    # transfer from smaller to larger bucket 
    if cur[1] > 0 and cur[0] < x:
        next = (min(x,cur[0]+cur[1]), max(0, cur[1]-(x-cur[0])))
        water_helper(x,y,z,next,moves, all_moves)
                

all_moves = []
water(4,3,2,(0,0),[],all_moves)
for p in all_moves:
    print p
