Hello, I'm developing a little work where I need to solve a puzzle, precisely this puzzle:
Itbasicallyconsistsofslidingthepiecestothewhitespaceinordertoleavetheminorder(orinthedesiredstate).
Example: link
But before starting the resolution, I need to check if it is solvable, I already researched but I can not understand why the inversion count guarantees this, and what those inversions are.
The part where I'll check this 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 already added it to the code.
The game grid setting is an array containing the sequence of numbers.
Ex.
[5, 3, 2, 1, 4, 6, 8, 7, ""]