I tried writing recursion to solve sudoku, and I'm having a problem with the recursion. If it's unsolvable its ok, but if it is solvable it is getting to infinite loop.
public static boolean recursion (int sodukuMatrix[][],int posRow, int posCol ,int i){
if (posRow==0 && posCol==0 && i==10)
return false;
if(there is existing number){
if (posCol==8 && posRow==8)
return true;
call recursion with next square
}
else {
i=sodukuMatrix[posRow][posCol]+1;
while (i<10){
if (function: if I put i at the current location it is ok){
sodukuMatrix[posRow][posCol]=i;
if (posCol==8 && posRow==8)
return true;
call recursion with next square
}
else
i++;
}
sodukuMatrix[posRow][posCol]=0;
return false;
}
return false;
}
}
if(rec (cloneMatrix, sodukuMatrix, posRow, posCol+1 ,i)) }, and, obviously, atif (function: if I put i at the current location it is ok){.