1
Hello, I am developing a small work where I need to solve a puzzle through algorithm A* more precisely this puzzle:
Basically it consists of sliding the pieces to the white space in order to keep them in order (or in the desired state).
Example: https://www.proprofs.com/games/puzzle/sliding/
But before starting the resolution, I need to check if it is solvable, I have researched but I can’t understand why the inversion count guarantees this, and what are these inversions.
The part where I’m gonna check this out is this:
function countInversions(array) {
// Contar inversões
// TODO
}
console.log(tiles);
return countInversions(tiles) % 2 == 0;
I saw that the result is acquired by counting the inversions and then capturing the module by 2, in case to find out if the result is odd or even, so I’ve already added to the code.
The game grid configuration is a array containing the sequence of numbers.
Ex.
[5, 3, 2, 1, 4, 6, 8, 7, ""]
The question of finding the odd and even I understood, the biggest doubt I have now is related to why this ensures that the problem is solvable, something more formal. Thank you for the reply!
– MSLacerda
@matheussoareslacerda edited the answer
– Pedro Augusto
That’s exactly what it was, man, thanks!
– MSLacerda