1

I have different numbers which needs to be sorted in ascending order by exchanging each number with 0.

The resultant number should be obtained only from exchanging each number with 0.

for eg, I have 3x3 matrix,

   3    4     1
   2    5     0
   6    8     7

In above matrix, the number should be exchanged with 0 to make an ascending order. From above, only 5,7 and 1 are exchanged with 0 at first step. The final output should be like this

   1    2     3
   4    5     6
   7    8     0

What is an optimal solution to achieve this.

Thanks

1
  • 2
    The algorithm procedure is not very clear Commented Jun 21, 2014 at 13:03

1 Answer 1

3

This is an easier version of the famous 15-puzzle.

The general way to approach such a problem is to model it as states graph, and run a shortest-path algorithm in order to find a path from the source (given board) to the target (sorted board).

The states graph is G=(V,E) where: V= { all possible boards } and E={(u,v) | can change board u to v with a single swap }`.

You can run BFS or bi-directional BFS (since you have one source and one target), or even A* Algorithm with an appropriate admissible heuristic function to find the path on the states graph, which represents a series of swaps that yield a solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.