5 ms·
Neat little problem! You could do SAT-solvers and stuff, but it's also obviously a graph search problem: each state is a vertex, each edge is an operation that
by OskarS 3y ago
Neat little problem! You could do SAT-solvers and stuff, but it's also obviously a graph search problem: each state is a vertex, each edge is an operation that leads to another vertex. So, obviously: Dijkstra works (or really just BFS, since the edges are unweighted), but not the fastest in the world.
The fun one to play with would be A-star, but you'd have to find a suitable heuristic. One obvious candidate is that if you have target list that is X long and a current list that is Y long, then you need at least abs(Y - X) steps to get there (since each step either adds or removes a number). That's admissable and would probably speed up the search quite a bit compared to regular BFS, but you could probably do a lot better. I'm thinking a heuristic based on the number of inversions or something.
I would suspect if you found a really good heuristic for A-star, that's about as good as you're going to get. Though maybe there's something more clever I'm not spotting.
- 61u 3y agoYou could just run bfs from both starting and ending nodes until you find a node reachable from both the starting and ending nodes.
- 61u 3y agohttps://ideone.com/MGFwdo https://ideone.com/MGFwdo I am running BFS from only the starting node because the graph is symmetrical.
- hnfong 3y agoHeuristic: Longest common subsequence of the current state vs the target?