生成器应用

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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
def preatprint(solution):
    def lines(pos,length=len(solution)):
        return '. '*(pos)+'x '+'. '*(length-pos-1)
    for pos in solution:
        print lines(pos)
        
if __name__=='__main__':
   
     import random
     preatprint(random.choice(list(queues(8))))