def ASearch(node_tup): queue = PriorityQueue() allNodes = set() queue.push((getDirtyNum(node_tup[:-1]), node_tup, "")) while not queue.empty(): node = queue.pop() tnode = node[2][1] res = isGoal(tnode, False) if res == 1: return node[2][2] elif res == -1: return None for idx, child in enumerate(getAllChildrenLargeEvn(tnode, allNodes)): if child != None: cost = node[0] - getDirtyNum(tnode[:-1]) final_val = getDirtyNum(child[:-1]) + (cost + 1) queue.push((final_val, child, node[2][2] + seq[idx])) return "None"
def depthLimitedSearch(node_tup, dep_limit=DEPTH_LIMIT): frontier = [] frontier.append([node_tup,""]) allNodes = set() while len(frontier) != 0: node = frontier.pop() tnode = node[0] res = isGoal(tnode,False) if res == 1: return "".join(node[1]) elif res == -1: return None allNodes.add(tnode) if len(node[1]) < dep_limit: for idx, child in enumerate(reversed(getAllChildrenLargeEvn(tnode, allNodes))): if child != None: frontier.append([child, node[1] + seq[-(idx+1)]]) return "None"
def depthFirstSearch(node_tup): frontier = [] frontier.append([node_tup,""]) allNodes = set() while len(frontier) != 0: node = frontier.pop() tnode = node[0] res = isGoal(tnode,False) if res == 1: return "".join(node[1]) elif res == -1: return None if tnode not in allNodes: allNodes.add(tnode) for idx, child in enumerate(reversed(getAllChildrenLargeEvn(tnode, allNodes))): if child != None: frontier.append([child, node[1] + seq[-(idx+1)]]) return "None"