Created
July 22, 2015 16:41
-
-
Save jerinphilip/9ba3581789326ba0b68e to your computer and use it in GitHub Desktop.
Sudoku Solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct sudoku{ | |
int A[9][9]; | |
bool FIXED[9][9]; | |
bool check_row(int x, int i, int j){ | |
//For a row, i=const | |
int jj; | |
for(jj=0; jj<9; jj++){ | |
if( A[i][jj] == x ) | |
return false; | |
} | |
return true; | |
} | |
bool check_col(int x, int i, int j){ | |
//For a col, j=const | |
int ii; | |
for(ii=0; ii<9; ii++){ | |
if ( A[ii][j] == x ) | |
return false; | |
} | |
return true; | |
} | |
bool check_sqr(int x, int i, int j){ | |
//For subsquare, start and end of row and col | |
int si, ei, sj, ej; | |
si = (i/3) * 3; | |
ei = si + 2; | |
sj = (j/3) * 3; | |
ej = sj + 2; | |
int ii, jj; //New looping variables. | |
for(ii=si; ii<=ei; ii++){ | |
for(jj=sj; jj<=ej; jj++){ | |
if ( A[ii][jj] == x ) | |
return false; | |
} | |
} | |
return true; | |
} | |
bool check_all(int x, int i, int j){ | |
if( check_row(x, i, j) && check_col(x, i, j) && check_sqr(x, i, j) ) | |
return true; | |
return false; | |
} | |
bool try_cell(int i, int j){ | |
int in, jn; | |
bool last_cell; | |
//Setting up next cell to be visited. | |
jn = (j+1)%9; | |
if (jn == 0) | |
in = (i+1)%9; | |
else | |
in = i; | |
//Checking if last cell or not. | |
if ( i==8 && j==8) | |
last_cell = true; | |
else | |
last_cell = false; | |
//In case the cell has a value that is fixed. | |
if ( FIXED[i][j] ){ | |
if ( last_cell ) | |
return true; | |
else | |
return try_cell(in, jn); | |
} | |
//The other case, obviously. | |
else{ | |
//Brute-force, naive method. | |
for(int x = 1; x <= 9; x++){ | |
if(check_all(x, i, j)){ | |
A[i][j] = x; | |
if ( last_cell ){ | |
return true; | |
} | |
else if(try_cell(in, jn)){ | |
return true; | |
} | |
} | |
} | |
A[i][j] = 0; | |
return false; | |
} | |
} | |
void solve(){ | |
bool solved = try_cell(0, 0); | |
if ( solved ){ | |
printf("Successfully Solved\n"); | |
print(); | |
} | |
else{ | |
printf("Unable to solve\n"); | |
} | |
} | |
void print(){ | |
int i, j; | |
for(i=0; i<9; i++){ | |
for(j=0; j<9; j++){ | |
printf("%d ", A[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
void input(){ | |
int i, j; | |
char S[20]; | |
for(i=0; i<9; i++){ | |
scanf("%s", S); | |
for(j=0; j<9; j++){ | |
A[i][j] = S[j]-'0'; | |
if ( A[i][j] != 0 ) | |
FIXED[i][j] = true; | |
else{ | |
FIXED[i][j] = false; | |
} | |
} | |
} | |
} | |
}; | |
int main(){ | |
sudoku S; | |
char grid[50]; | |
int TC = 50, tc, sum=0; | |
for(int t = 1; t <= TC; t++){ | |
scanf("%s %d", grid, &tc); | |
printf("%s %d\n", grid, tc); | |
S.input(); | |
S.solve(); | |
sum += S.A[0][0]*100 + S.A[0][1]*10 + S.A[0][2]; | |
} | |
printf("Total sum = %d\n", sum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment