Could anyone help with this Sudoku brute force recursive solver? (shown below): It gets caught in a loop I think. I've also included the algorithm outline I use (taken from wikipedia), and the input text file ('sudokuInput.txt') I'm trying is:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
const int n = 9, p = 9;
void printSudoku( const int sudokuBoard[n][p], bool printToFile );
void getSudoku( int sudokuBoard[n][p], int sudokuMask[n][p] );
bool violationCheck( int sudokuBoard[n][p], const int sudokuMask[n][p], int row, int col, int number );
void solveIndividualCell( int sudokuBoard[n][p], const int sudokuMask[n][p], int row, int col, int startLoopFromHere );
// is sudokuBoard[row][col] = number OK?
// Yes -> return violation(true): meaning number can't fit in location, check number+1..
// No -> return violation(false): meaning number is OK to fit in location
int main()
{
bool solvedYet = false;
int i, j;
int sudokuBoard[n][p], sudokuMask[n][p];
// bool violationCheck( ..)
// is number violation?
// Yes -> return violation(true)
// No -> return violation(false)
bool violationCheck( int sudokuBoard[n][p], const int sudokuMask[n][p], int row, int col, int number )
{
int i, j;
cout << "sudokuBoard[" << row << "][" << col << "] = " << sudokuBoard[row][col];
cout << " - try[" << number << "]: ";
/* When checking for violations, it is discovered that the "1" is not allowed,
so the value is advanced to a "2".. If a cell is discovered where none of the 9 digits is allowed,
then the algorithm leaves that cell blank and moves back to the previous cell.
The value in that cell is then incremented by one.
*/
void solveIndividualCell(int sudokuBoard[n][p], const int sudokuMask[n][p], int row, int col, int startLoopFromHere )
{
bool violation = false;
bool cellSuccess = false;
int violationCounter = 0;
int maxAllowedViolations = 10 - startLoopFromHere;
// at this point we know that numbers 1-9 dont fit into sudokuBoard[row][col]
// we now have to find the last place number placed on the board and adjust it:
if( violationCounter == maxAllowedViolations )
{
int tempRow, tempCol, tryValue;
cout << endl << endl;
cout << "** Backtrack Required for cell[" << row << "][" << col << "] **" << endl;
printSudoku( sudokuBoard, false);
cout << "Search for previous (suitable) cell:" << endl;