My Weblog

Blog about programming and math

Sudoku solver

Solving sudoku is having fun ,thinking recursively and keep thinking until you get solution. As being a programmer i tried to solve sudoku using computational power but till now my code can solve only easy sudoku so i have try some fancy techniques as given on http://en.wikipedia.org/wiki/Algorithmics_of_sudoku . For solving hard sudoku we must have to use DFS technique but till now i can’t figure out.

				
	int main()
		{
			int mat[10][10];
			for(int i=1;i<=9;i++)
				for(int j=1;j0)
			 {
				prepro=0;
				for(int i=1;i<=9;i++)
					for(int j=1;j<=9;j++)
						if(mat[i][j]==0)
						 {
							int igp=((i-1)/3)*3;
							int jgp=((j-1)/3)*3;
							for(int w=1;w<=9;w++)
								tmp1[w]=1;
							for(int m=1;m<=9;m++)//row
								if(mat[i][m])
									tmp1[mat[i][m]]=0;
							for(int m=1;m<=9;m++)//column
								if(mat[m][j])
									tmp1[mat[m][j]]=0;
							for(int m=1;m<=3;m++)//box
								for(int n=1;n<=3;n++)
									if(mat[m+igp][n+jgp])
										tmp1[mat[m+igp][n+jgp]]=0;
							int count=0,sol;
							for(int m=1;m<=9;m++)
								if(tmp1[m])
									count++,sol=m;
						
							if(count==1)//only one possible solution
							 mat[i][j]=sol,prepro=1;
						 }
			}
		//printf("\n");
		for(int i=1;i<=9;i++)
			for(int j=1;j<=9;j++)
				printf("%d%c",mat[i][j],(j<9)?' ':'\n');
			
		}
Advertisements

February 25, 2008 - Posted by | Programming

1 Comment »

  1. If you manage to solve trigonpuzzles using computer power, could you let me know.
    You might be able to solve the simple ones but the more difficult ones are impossible to solve using an algorithm – remember there is only one correct answer
    Accept the challenge
    Lester Jordan

    Comment by Lester Jordan | February 25, 2008 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: