This is a Leetcode problem -:
Here is my solution to this challenge -:
Explanation -
Convert the board to a list so that we can have a visit set to track which state is visited.
Construct
Construct an adjacency list to mark which position we can go to. For example, [[1, 2, 3], [4, 5, 0]], as it is a board value, 1 can swap with 4 or 2.
If we make it a string "123450", that means position 0 (so-called index) can swap with index value 0 and index value 3 => 0:[1, 3], same for 1:[0, 2, 4] for so on so forth.
Now
Now that we have the graph, we just need to do a regular BFS.
Here are some example outputs -:
Here are the times taken for each output -:
Here is my Leetcode result (32 test cases) -:
So, I would like to know whether I could make my program shorter and more efficient.
Any help would be highly appreciated.