def solution(initial): buildDictOfStates(initial) buildSymTbls() G = buildGraph() source = symTbl[tuple(initial)] node = symTbl[tuple(final)] abfs = BFS(G, source) # path is represented as a list of vertices path = abfs.pathTo(node) if path is None: return "No solution" # actions = [ (1, 2), (2, 3) ] actions = buildActions(path) dist = abfs.distTo(node) print dist for action in actions: print '{0} {1}'.format(action[0], action[1]) return dist, actions
G.addEdge(v, w) return G def buildActions(path): actions = [] for i in range(len(path)-1): from_state = path[i] to_state = path[i+1] # convert state from vertex to form [n1, n2, ...] from_state = symTbl2[from_state] to_state = symTbl2[to_state] for e in d[from_state]: if e[0] == to_state: actions.append(e[1]) return actions buildDictOfStates(initial) buildSymTbls() G = buildGraph() source = symTbl[tuple(initial)] node = symTbl[tuple(final)] abfs = BFS(G, source) # path is represented as a list of vertices path = abfs.pathTo(node) if path is None: print "No solution" # actions = [ (1, 2), (1, 3) ] actions = buildActions(path) dist = abfs.distTo(node) print dist for action in actions: print '{0} {1}'.format(action[0], action[1])
def buildActions(path): actions = [] for i in range(len(path) - 1): from_state = path[i] to_state = path[i + 1] # convert state from vertex to form [n1, n2, ...] from_state = symTbl2[from_state] to_state = symTbl2[to_state] for e in d[from_state]: if e[0] == to_state: actions.append(e[1]) return actions buildDictOfStates(initial) buildSymTbls() G = buildGraph() source = symTbl[tuple(initial)] node = symTbl[tuple(final)] abfs = BFS(G, source) # path is represented as a list of vertices path = abfs.pathTo(node) if path is None: print "No solution" # actions = [ (1, 2), (1, 3) ] actions = buildActions(path) dist = abfs.distTo(node) print dist for action in actions: print '{0} {1}'.format(action[0], action[1])