I would like to know how to make a code that lets me know if a puzzle n xn (in which I generate random positions [and leave a picture of the game]) has a solution, so I saw not all the different possibilities of generating A puzzle has a solution, I found a code that supposedly told me if it was solvable or not, but I compared results with an online verifier and it's not 100% effective.
//Code
public boolean isSolvable(List<Integer> puzzle, int squartRoot) {
int parity = 0;
int gridWidth = squartRoot;
int row = 0;
int blankRow = 0;
for (int i = 0; i < puzzle.size(); i++) {
if (i % gridWidth == 0) {
row++;
}
if (puzzle.get(i) == 0) {
blankRow = row;
continue;
}
for (int j = i + 1; j < puzzle.size(); j++)
{
if (puzzle.get(i) > puzzle.get(j) && puzzle.get(j) != 0)
{
parity++;
}
}
}
if (gridWidth % 2 == 0) {
if (blankRow % 2 == 0) {
return parity % 2 == 0;
} else {
return parity % 2 != 0;
}
} else {
return parity % 2 == 0;
}
}
// Photo of the game
The idea is that it can serve as much 3 x3 as 4 x4 etc u_u "did not manage to make it work correctly