Thank you for your appreciation.
Here's the code for the iterative version of the solver:
Code:
BOOL CSudokuSolver::Solve(SUDOKUTBL & tbl)
{
Reset();
Init(tbl); // initialize
BYTE iDigit,
idx,
arrDigitStack[81],
arrAssignedStack[81];
ZeroMemory(&arrDigitStack ,sizeof arrDigitStack);
ZeroMemory(&arrAssignedStack,sizeof arrAssignedStack);
idx = 0;
BYTE bFall(0);
while( idx < m_cTblIdx )
{
if( m_lpfnDebug )
m_lpfnDebug(&m_tblSudoku,m_arrTblIdx[idx].iRow,m_arrTblIdx[idx].iCol);
if( arrDigitStack[idx] )
{
// Unassign previous assigned digit
if( arrAssignedStack[idx] )
{
UnassignDigit(
m_arrTblIdx[idx].iRow,
m_arrTblIdx[idx].iCol,
arrDigitStack[idx]);
arrAssignedStack[idx] = 0;
}
// No more digits to assign - fallback
if( arrDigitStack[idx] == 9 )
goto fallback;
}
// Try assigning a digit
for( iDigit = arrDigitStack[idx]+1; iDigit < 10 ; iDigit ++ )
{
if( IsDigitAssigned(
m_arrTblIdx[idx].iRow,
m_arrTblIdx[idx].iCol,
iDigit) )
continue;
AssignDigit(
m_arrTblIdx[idx].iRow,
m_arrTblIdx[idx].iCol,
iDigit);
arrDigitStack [idx] = iDigit; // assigned
arrAssignedStack[idx] = 1;
// Advance
goto nextdigit;
}
fallback:
// Reset stack
arrAssignedStack[idx] = 0;
arrDigitStack[idx] = 0;
// Fallback
if( !idx )
return FALSE;// no solution
idx --;
continue;
nextdigit:
// Climb
idx ++;
}
if( m_lpfnSolution )
m_lpfnSolution( &m_tblSudoku );
m_bSolved = TRUE;
return TRUE;
}
It improves the speed of the implementation. Cheers.