lzyerste
3/23/2014 - 8:57 AM

queen solution in python with yield

queen solution in python with yield

def conflict(state, nextX):
    nextY = len(state)
    for i in range(nextY):
        if abs(state[i] - nextX) in (0, nextY - i):
            return True
    return False

def queues(num = 8, state = ()):
    for pos in range(num):
        if not conflict(state, pos):
            if len(state) == num - 1:
                yield (pos,)
            else:
                for result in queues(num, state + (pos,)):
                    yield (pos,) + result