def puzzle(parentNode,history_boards): global possibleMoves nodes = 0 while(True): if reachGoal(parentNode,goal_state): print "Congradulations! You reach goal after "+str(history_boards[toInt(parentNode)][2])+" moves" print "Total nodes expanded: ",nodes print "\n\nThe move process replay blow: \n" num = toInt(parentNode) while True: print "\nAfter "+str(history_boards[num][2])+" moves you got: " print showBoard(num) num = history_boards[num][1] if history_boards[num][2] == 0: print "\nAfter "+str(history_boards[num][2])+" moves you got: " print showBoard(num) return else: possible_moves = possibleMoves(parentNode) for move_tuple in possible_moves: tmp = parentNode[:] sonNode = swap(tmp,move_tuple) if toInt(sonNode) not in history_boards: nodes +=1 layer = history_boards[toInt(parentNode)][2] history_boards[toInt(sonNode)] = (cost(sonNode,layer+1),toInt(parentNode),layer+1) history_boards[toInt(parentNode)] = (sys.maxint,history_boards[toInt(parentNode)][1],history_boards[toInt(parentNode)][2]) parentNode = toList(min(history_boards.iteritems(),key=operator.itemgetter(1))[0])
print "Congradulations! You reach goal after "+str(history_boards[toInt(parentNode)][2])+" moves" print "Total nodes expanded: ",nodes print "\n\nThe move process replay blow: \n" num = toInt(parentNode) while True: print "\nAfter "+str(history_boards[num][2])+" moves you got: " print showBoard(num) num = history_boards[num][1] if history_boards[num][2] == 0: print "\nAfter "+str(history_boards[num][2])+" moves you got: " print showBoard(num) return else: possible_moves = possibleMoves(parentNode) for move_tuple in possible_moves: tmp = parentNode[:] sonNode = swap(tmp,move_tuple) if toInt(sonNode) not in history_boards: nodes +=1 layer = history_boards[toInt(parentNode)][2] history_boards[toInt(sonNode)] = (cost(sonNode,layer+1),toInt(parentNode),layer+1) history_boards[toInt(parentNode)] = (sys.maxint,history_boards[toInt(parentNode)][1],history_boards[toInt(parentNode)][2]) parentNode = toList(min(history_boards.iteritems(),key=operator.itemgetter(1))[0]) if __name__ == "__main__": history_boards = {} history_boards[toInt(initial_state)]=(cost(initial_state,0),0,0) puzzle(initial_state,history_boards)