Posts

Showing posts from November 2, 2011

C Program to solve a 9*9 Sudoku

Here is a program that I wrote to solve Sudoku sometime back. Posting on the blog now. #include int show_sudoku(); static int sudoku_nos[9][9]= { {3,0,5,4,0,1,6,0,2}, {0,0,0,0,0,0,0,5,1}, {6,0,0,0,0,9,7,0,0}, {0,0,0,0,0,0,0,0,4}, {0,0,0,6,4,5,0,0,9}, {4,3,7,0,0,8,2,0,5}, {0,0,0,0,6,2,0,0,0}, {0,5,4,0,1,0,0,8,6}, {8,7,0,0,5,0,1,2,0} }; static short int vals_g[]={1,2,3,4,5,6,7,8,9}; static short int others_options[]={1,2,3,4,5,6,7,8,9}; int show_possiblities(int i, int j,short int vals[]) { int m=0,n=0,count=9,udi=0; for(m=0;m<9;m++) { if(sudoku_nos[i][m]!=0) { vals[sudoku_nos[i][m]-1]=0; count--; } } for(m=0;m<9;m++) { if((sudoku_nos[m][j]!=0)&& vals[sudoku_nos[m][j]-1]!=0) { vals[sudoku_nos[m][j]-1]=0; count--; } } for(m=(i-(i%3));m<((i-(i%3))+3);m++) {    for(n=(j-(j%3));n<((j-(j%3))+3);n++) { if((sudoku_nos[m][n]!=0)&& vals[sudoku_nos[m][n]-1]!=0) { vals[sud