Пример #1
0
def main():
    params = readParams('load.txt')
    boardHeight = params['boardHeight']
    boardLength = params['boardLength']
    boardMax    = boardHeight*boardLength
    pieces = convertPiecesToDict(params['boardPieces'],boardLength)
    
    # We do a breath-first search across the combinations.
    # Branches where a piece does not fit are not pursued saving
    # computations. This approach is better for larger puzzles.
    
    numSolutions = 0
    solutions = []
    frontier = Queue.Queue()
    
    #Initialize the frontier
    for i in pieces.keys():
        frontier.put(i)
        
    explored = set()
    options = ''.join(pieces.keys())

    while True:
        if frontier.empty():
            break
        node = frontier.get()
        ret = doPiecesFit(node,pieces,boardHeight,boardLength)
        explored.add(node)
        if type(ret) is int and ret == 1:
            # Expand the viable node.
            cl = options.translate(None,node)
            for x in cl:
                newnode = node+x
                # Add the resulting new nodes to frontier.
                if newnode not in explored:
                    frontier.put(newnode)
        elif type(ret) is list:
            if not wasSolutionPreviouslyComputed(ret,solutions):
                pp3.printBoard(boardLength,boardHeight,ret)
                numSolutions = numSolutions+1
                solutions.append(ret[:])
                		
    print 'Number of solutions: %d' % (numSolutions)
Пример #2
0
def main():
    params = readParams('load.txt')
    boardHeight = params['boardHeight']
    boardLength = params['boardLength']
    boardMax    = boardHeight*boardLength
    pieces = convertPiecesToDict(params['boardPieces'],boardLength)
    numSolutions = 0
    solutions = []
    
    # We need to use sorted keys to keep the output order identical to professor's.
    for combo in pp3.getAllOrderings(sorted(pieces.keys())):
    
        # Start with a fresh board for each combination
        contents = [0]*boardMax
        fancyContents = ['']*boardMax
        
        # Go through each piece and place in the next available free slot on the board
        for pieceLabel in combo:
            shiftedShape = nextFreeSlot(pieces[pieceLabel],boardHeight,boardLength,contents)
            
            # If a certain shape doesn't fit, go to the next combo.
            if not shiftedShape:
                break;
            
            #Place piece on the board by updating contents
            for i in shiftedShape:
                contents[i-1] = 1
                fancyContents[i-1] = pieceLabel
            
            #Check whether we have a solution
            if isBoardFilled(contents) and not wasSolutionPreviouslyComputed(fancyContents,solutions):
                pp3.printBoard(boardLength,boardHeight,fancyContents)
                solutions.append(fancyContents[:])
                numSolutions = numSolutions + 1
            
    print 'Number of solutions: %d' % (numSolutions)