def solveIDFS(game, over, dir): nivel_limite = 0 max = 0 while True: stack = [game] repetidos.clear() dis.clear() dis[game] = 0 while not len(stack) == 0: node = stack.pop(0) if AuxFunctions.isSolution(list(node), list(over)): print "Tiveram em dada altura um maximo de", max, "nos na fila." return nivel_limite index = node.index('0') if dis.get(node) < nivel_limite: for value in dir[index]: nodeTrader = list(node)[:] nodeTrader[index+value], nodeTrader[index] = nodeTrader[index], nodeTrader[index+value] nodeTraderString = AuxFunctions.returnString(nodeTrader) if not caminho.get(nodeTraderString): caminho[nodeTraderString] = node if not dis.get(nodeTraderString) or dis.get(node) + 1 < dis.get(nodeTraderString): dis[nodeTraderString] = dis.get(node) + 1 stack.insert(0, nodeTraderString) print len(stack) if len(stack) > max: max = len(stack) nivel_limite += 1
def solveAStar(game, over, dir): distancias = [] heap.heapify(distancias) max = 0 gameString = AuxFunctions.returnString(game) dis[gameString] = 0 caminho[gameString] = gameString heap.heappush(distancias, (manhattanDistance(game, over), game)) while not len(distancias) == 0: node = heap.heappop(distancias) repetidos[AuxFunctions.returnString(node[1])] = 1 if AuxFunctions.isSolution(node[1], over): print "Tiveram em dada altura um maximo de", max, "nos na fila." return distancia index = node[1].index(0) for value in dir[index]: node1 = node[1][:] node1String = AuxFunctions.returnString(node1) distancia = dis.get(node1String) + 1 distanciaTemp = dis.get(node1String) node1[index+value], node1[index] = node1[index], node1[index+value] node1String = AuxFunctions.returnString(node1) dis[node1String] = distanciaTemp + 1 distancia += manhattanDistance(node1, over) if not caminho.get(node1String): caminho[node1String] = AuxFunctions.returnString(node[1]) if not repetidos.get(node1String): heap.heappush(distancias, (distancia, node1)) if len(distancias) > max: max = len(distancias)
def solveDFS(game, over, dir): tabGame = AuxFunctions.returnString(game) tabTemp = AuxFunctions.returnString(game) stack = [tabGame] max = 0 dis[tabGame] = 0 while not len(stack) == 0: game = list(stack.pop(0)) tabGame = AuxFunctions.returnString(game) # printTable(game) index = game.index("0") if not repetidos.get(tabGame): repetidos[tabGame] = 1 for value in dir[index]: tempGame = game[:] tempGame[index + value], tempGame[index] = tempGame[index], tempGame[index + value] tabTemp = AuxFunctions.returnString(tempGame) stack.insert(0, tabTemp) if max < len(stack): max = len(stack) dis[tabTemp] = dis.get(tabGame) + 1 if AuxFunctions.isSolution(tempGame, over): print "Tiveram em dada altura um maximo de", max, "nos na fila." return dis.get(tabTemp)
def solveBFS(game, over, dir): tabGame = AuxFunctions.returnString(game) queue = [tabGame] dis[tabGame] = 0 max = 0 while not len(queue) == 0: game = list(queue.pop(0)) tabGame = AuxFunctions.returnString(game) index = game.index('0') if not repetidos.get(tabGame): repetidos[tabGame] = 1 for value in dir[index]: tempGame = game[:] tempGame[index+value], tempGame[index] = tempGame[index], tempGame[index+value] tabTemp = AuxFunctions.returnString(tempGame) dis[tabTemp] = dis[tabGame] + 1 queue.append(tabTemp) if len(queue) > max: max = len(queue) if not caminho.get(tabTemp): caminho[tabTemp] = tabGame if AuxFunctions.isSolution(tempGame, over): print "Tiveram em dada altura um maximo de", max, "nos na fila." return dis.get(tabTemp)