Okay so this has been driving me crazy. I have been working on a sudoku solver and through one of the topics posted on this site (http://www.cplusplus.com/forum/beginner/48341/) i found a backtracking sudoku solver (and made some modifications to it), only that it overwrites my numbers in the array. The backtacking code is below and i read in those exact numbers from a file and input them in the array. After doing so i print the board and then i call the solvePuzzle() function. And when its done i call the print function again to print the "solution". i have tried to put if(check(x,y,num) && arr[x][y] == 0) , but that puts me in an endless loop. Any ideas? (btw check function checks the row/col/3x3 grid for availability)
You can't backtrack without using recursion or a stack structure to hold state information.
In fact, if you read that post, the poster is saying that the code doesn't work.
Well the function works. The poster's code doesn't work but the person who replied with the correction to the code works perfectly fine. This is my output (but as you can tell the original numbers have been changed)
I just tested it and I killed it after it ran for about two minutes (my computer can solve a worst case Sudoku puzzle in under a minute thirty seconds). I see no way it could work, since it has no way to tell user input from solution data when it overwrites previous incorrect results when backtracking. This leads me to believe that the code merely counts up with a decimal number of 81 digits, hence why it was taking so long.
Again, backtracking requires recursion or some form of a stack. A single flag doesn't hold enough state to solve this problem with backtracking.
Ok i looked at the link you posted and i am using the implementation in there. I just have one question i know you declare sudoku matrix = { numbers of sudoku }; but i was wondering is there a way i can use the following to input the numbers in matrix?
1 2 3 4 5 6 7 8 9 10 11 12 13
sudoku matrix;
ifstream fin("sudata.dat");
if (!fin) {
cout << "Opening File Failed!" << endl;
}
else {
for (int r = 0; r < SIZE*2; r++) {
for (int c = 0; c < SIZE; c++) {
fin >> matrix[r][c];
}
}
}