I'm trying to write an algorithm, which solves an extended Sudoku. I managed to find the solution for 9x9 Sudoku, but when I tried to extend it my program returns "No solution" and I don't have an idea what is wrong. Maybe someone can suggest where I made a mistake?
#include<stdio.h>
#include<algorithm>
int sudoku[15][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 9},
{4, 7, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 5, 6, 2, 0, 0, 3},
{0, 6, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 4, 0, 0, 3, 0, 6, 0},
{0, 5, 9, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 2, 0, 0, 0, 0, 0},
{6, 0, 0, 4, 0, 0, 0, 0, 0},
{0, 4, 8, 0, 0, 0, 6, 0, 0},
{0, 0, 4, 0, 0, 0, 0, 0, 0},
{0, 2, 0, 0, 0, 0, 1, 0, 0},
{0, 9, 1, 0, 0, 4, 0, 0, 5},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 0, 0, 6, 0, 0, 0, 0, 5},
{5, 0, 0, 0, 0, 0, 8, 6, 0}};
bool isPossibleToInsert(int array[][9], int v, int x, int y){
int rows = (x / 3) * 3;
int columns = (y / 3) * 3;
for (int i=0; i<9; i++){
if (i < 3){
for (int j=0; j<7; ++j){
if (sudoku[j][y] == v) return false;
}
}
if (i > 5){
for (int j=8; j<=14; ++j){
if (sudoku[j][y] == v) return false;
}
}
if (array[x][i] == v) return false;
if (array[i][y] == v) return false;
if (array[rows + i%3][columns + i/3] == v) return false;
}
return true;
}
bool checkNextCell(int orginal[][9], int copy[][9], int x, int y);
bool tryToSolve(int sudoku[][9], int temp[][9], int x_val, int y_val){
if (sudoku[x_val][y_val] == 0){
for (int i=1; i<=9; ++i){
if (isPossibleToInsert(temp,i,x_val,y_val)){
temp[x_val][y_val] = i;
if (checkNextCell(sudoku,temp,x_val,y_val)) return true;
}
}
temp[x_val][y_val] = 0;
return false;
}
return checkNextCell(sudoku,temp,x_val,y_val);
}
bool checkNextCell(int orginal[][9], int copy[][9], int x, int y){
if ((x == 8) && (y == 8)) return true;
else if (x == 8) return tryToSolve(orginal,copy,0,y+1);
else return tryToSolve(orginal,copy,x+1,y);
}
int main(){
/*int sudoku[15][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 9},
{4, 7, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 5, 6, 2, 0, 0, 3},
{0, 6, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 4, 0, 0, 3, 0, 6, 0},
{0, 5, 9, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 2, 0, 0, 0, 0, 0},
{6, 0, 0, 4, 0, 0, 0, 0, 0},
{0, 4, 8, 0, 0, 0, 6, 0, 0},
{0, 0, 4, 0, 0, 0, 0, 0, 0},
{0, 2, 0, 0, 0, 0, 1, 0, 0},
{0, 9, 1, 0, 0, 4, 0, 0, 5},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 0, 0, 6, 0, 0, 0, 0, 5},
{5, 0, 0, 0, 0, 0, 8, 6, 0}};*/
int arrayMain[9][9];
std::copy(sudoku+7, sudoku+15, arrayMain);
if (tryToSolve(sudoku,arrayMain,0,0)){
for (int i=0; i<9; ++i){
for (int j=0; j<9; ++j){
printf("%d ",arrayMain[i][j]);
}printf("\n");
}
}
else{
printf("No solution");
}
return 0;
}
EDIT:
I'm sorry I didn't explain the problem properly. In the example above, the Sudoku has to be two separate 9x9 Sudokus (rows 1-9 it's first and 7-15 the another).