Created
January 30, 2013 13:17
-
-
Save taroyabuki/4673245 to your computer and use it in GitHub Desktop.
Sudoku Solver in C. http://codepad.org/1DkmFVXQ http://blog.unfindable.net/archives/2869
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 <stdio.h> | |
#define N 9 | |
#define B 3 //square root of N | |
int canBePlaced(int board[], int pos, int x) | |
{ | |
int row=pos/N; | |
int col=pos%N; | |
int i, j, topLeft; | |
for (i=0; i<N; ++i) { | |
if (board[row*N+i]==x) return 0; | |
if (board[col+i*N]==x) return 0; | |
} | |
topLeft=N*(row/B)*B+(col/B)*B; | |
for (i=0; i<B; ++i) { | |
for (j=0; j<B; ++j) { | |
if (board[topLeft+i*N+j]==x) return 0; | |
} | |
} | |
return 1; | |
} | |
void check(int board[], int pos) | |
{ | |
int i, x, newPos; | |
//Solution is found. | |
if (pos==N*N) { | |
for (i=0; i<N*N; ++i) { | |
printf("%d ", board[i]); | |
if (i%N==N-1) printf("\n"); | |
} | |
printf("\n"); | |
return; | |
} | |
//Find a blank. | |
for (newPos=pos; newPos<N*N; ++newPos) { | |
if (board[newPos]==0) break; | |
} | |
//Check recursively. | |
for (x=1; x<=N; ++x) { | |
if (canBePlaced(board, newPos, x)) { | |
board[newPos]=x; | |
check(board, newPos+1); | |
board[newPos]=0;//backtracking | |
} | |
} | |
} | |
int main() | |
{ | |
int board[]={ | |
1,0,0,0,0,7,0,9,0, | |
0,3,0,0,2,0,0,0,8, | |
0,0,9,6,0,0,5,0,0, | |
0,0,5,3,0,0,9,0,0, | |
0,1,0,0,8,0,0,0,2, | |
6,0,0,0,0,4,0,0,0, | |
3,0,0,0,0,0,0,1,0, | |
0,4,0,0,0,0,0,0,7, | |
0,0,7,0,0,0,3,0,0}; | |
check(board, 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment