0

Given a goal state

int final[3][3]={{1,2,3},
                 {4,5,6},
                 {7,8,9}};

and a random initial state, I want to sort my array as final only by shifting rows (right or left) and columns (up and down) of my table

   7 8 4    by shifting to the right the first row it will become 4 7 8
   2 1 9                                                          2 1 9
   6 5 3                                                          6 5 3

So I want to use a* search and I'm trying to find a good heuristic.

I've already tried with misplaced array elements.

Any suggestions?

Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

2

I view this as an algebraic problem. You are given a group of permutation which is generated by 6 cycles (3 rows and 3 columns) and you want to find some more moves which help you to get to any permutation.

First advice: not all permutations are possible! Since every shift is an even permutation (a 3-cycle is the composition of two transpositions) only even permutations are possible. Hence you will not find any solution to a configuration where all is in place but two swapped numbers as in (2,1,3),(4,5,6),(7,8,9).

Second advice. If r is a row shift and c is a coumn shift, compute the action of rcr'c' where r' and c' are the inverse shifts. This "commutator" is again a cycle of 3 elements but this time they are not in a row or column. By choosing different r and c you get a lot of 3-cycles which can be used in the third advice.

Third advice. Consider the region of numbers which are already in their final position. Apply 3-cycles to the complement of this set to reduce it, until you get to a solution.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • My thought was that if I expand the tree of the initial array, I will have 12 possible moves(shifts) to do, so I'll choose the one with the minimum f=g+h. Now, my first problem is to find a better heuristic and my second problem is if I use 4x4 array or 5x5 array how can I compute the g cost. – user3601268 May 22 '14 at 09:21
  • I don't know what f,g,h are... My advice is to study the problem from an abstract mathematical point of view, not with a blind search in the tree of possible moves. – Emanuele Paolini May 22 '14 at 19:23