Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

//SUDOKU SOLVER #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.

h> class sudoku { int a[9][9],b[81]; public: void input(); void solve(); void show(); }ob; void sudoku::input() { cout<<"\nEnter each line of the puzzle"; cout<<"\nIf a square is empty input 0"; cout<<"\nFor example 090000506"; char line[10]; int i,j,p=0; for(i=1;i<=9;i++) { cout<<"\nEnter line "<<i<<" : "; gets(line); for(j=0;j<9;j++) { char ch=line[j]; b[p++]=((int)ch)-48; cout<<((int)ch)-48<<" "; } } p=0; for(i=0;i<9;i++) for(j=0;j<9;j++) a[i][j]=b[p++]; } void sudoku::solve() { int i,j,k,ri,ci,sr,sc; /*If i is the index of the 1D array, then ri and ci are the row and column of the equivalent pos of the 2D array. sr and sc are the row and column of the top left element the cell's 3x3 square. */ int flag; for(i=0;i<81;i++) { ri=i/9; ci=i%9; if(b[i]!=0) continue; do { flag=1; //made true as if the value in a[ri][ci] is valid; a[ri][ci]++; //Increment to try the next value. 1st time it becomes 1 //row check if an duplicate is found then flag is made false for(j=0;j<9;j++) { if(j!=ci&& a[ri][ci]==a[ri][j]) { flag=0; break;

} } //column check if an duplicate is found then fl ag is made false for(j=0;j<9;j++) { if(j!=ri&& a[ri][ci]==a[j][ci]) { flag=0; break; } } //square check if an duplicate is found then fl ag is made false sr=(ri/3)*3; sc=(ci/3)*3; for(j=0;j<3;j++) { for(k=0;k<3;k++) { && a[ri][ci]==a[sr+j][sc+k]) { flag=0; break; } } if(flag==0) break; } }while(flag==0 && a[ri][ci]<10); if(a[ri][ci]==10) { a[ri][ci]=0; i-=2; while(b[i+1]!=0) i--; } } } void sudoku::show() { int i,j; cout<<"\n"; for(i=0;i<9;i++) { for(j=0;j<9;j++) cout<<a[i][j]<<"\t"; cout<<"\n\n"; } } void main() { clrscr(); ob.input(); ob.solve(); ob.show(); getch();

if((sr+j!=ri sc+k!=ci)

http://filesflash.com/b6zev28f

Love is what ur parents give you if u clear the IIT exams Ref No. BIS :13921 http://www.authorstream.com/Presentation/rahul11-795468-power-factor-measurement -and-correction/

You might also like