Created
October 2, 2015 14:08
-
-
Save lpraat/fd4af0ebcf399bbf5f52 to your computer and use it in GitHub Desktop.
Sudoku solver
This file contains hidden or 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 <stdio.h> | |
| #define ORDER 3 | |
| #define DIM ORDER*ORDER | |
| int checkLine(int m[DIM][DIM], int line) | |
| { | |
| int i, j; | |
| for (i = 0; i < DIM-1; i++) | |
| for (j = i+1; j < DIM; j++) | |
| if (m[line][i] == m[line][j] && | |
| m[line][i] > 0) | |
| return 0; | |
| return 1; | |
| } | |
| int checkColumn(int m[DIM][DIM], int column) | |
| { | |
| int i, j; | |
| for (i = 0; i < DIM-1; i++) | |
| for (j = i+1; j < DIM; j++) | |
| if (m[i][column] == m[j][column] && | |
| m[i][column] > 0) | |
| return 0; | |
| return 1; | |
| } | |
| int checkSub(int m[DIM][DIM], int r, int c) | |
| { | |
| int v[DIM], i, j, k=0; | |
| for (i = r; i < r+ORDER; i++) | |
| for (j = c; j < c+ORDER; j++) { | |
| v[k] = m[i][j]; | |
| k++; | |
| } | |
| for (i = 0; i < DIM; i++) | |
| for (j = i+1; j < DIM; j++) | |
| if (v[i] == v[j] && v[i] > 0) | |
| return 0; | |
| return 1; | |
| } | |
| int checkSubMatrix(int m[DIM][DIM]) | |
| { | |
| int r, c; | |
| for (r = 0; r < DIM; r += ORDER) | |
| for (c = 0; c < DIM; c += ORDER) | |
| if (checkSub(m, r, c)==0) | |
| return 0; | |
| return 1; | |
| } | |
| int checkGame(int m[DIM][DIM]) | |
| { | |
| int i; | |
| for (i = 0; i < DIM; i++) | |
| if (checkLine(m, i) == 0 || | |
| checkColumn(m, i) == 0 || | |
| checkSubMatrix(m) == 0 ) | |
| return 0; | |
| return 1; | |
| } | |
| void printMatrix(int m[DIM][DIM]) | |
| { | |
| int i, j; | |
| for (i = 0; i < DIM; i++) { | |
| for (j = 0; j < DIM; j++) | |
| printf("%i\t", m[i][j]); | |
| printf("\n"); | |
| } | |
| } | |
| int complete(int m[DIM][DIM], int *x, int *y) | |
| { | |
| int i, j; | |
| for (i = 0; i < DIM; i++) | |
| for (j = 0; j < DIM; j++) | |
| if (m[i][j] == 0) { | |
| *x = i; | |
| *y = j; | |
| return 0; | |
| } | |
| return 1; | |
| } | |
| int solveGame(int m[DIM][DIM]) | |
| { | |
| int x, y, i; | |
| if (checkGame(m) == 0) | |
| return 0; | |
| if (complete(m, &x, &y) == 1) { | |
| if (checkGame(m) == 0) | |
| return 0; | |
| else | |
| return 1; | |
| } | |
| for (i = 1; i <= DIM; i++) { | |
| m[x][y] = i; | |
| if (solveGame(m) == 1) | |
| return 1; | |
| } | |
| m[x][y] = 0; | |
| return 0; | |
| } | |
| int main(void) | |
| { | |
| int m[DIM][DIM] = { | |
| {0,0,1,5,0,0,0,0,0}, | |
| {4,0,0,0,0,0,0,7,0}, | |
| {0,6,0,0,9,0,1,0,0}, | |
| {3,0,0,0,0,1,5,0,0}, | |
| {0,9,0,0,6,0,0,0,8}, | |
| {0,0,5,7,0,0,0,4,0}, | |
| {0,8,0,1,0,0,0,0,2}, | |
| {0,0,3,0,0,0,0,5,0}, | |
| {0,0,0,0,0,2,6,0,0} | |
| }; | |
| solveGame(m); | |
| printMatrix(m); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment