Thursday, November 17, 2011

Obfuscated Sudoku Solving

While hunting on the internet, I found out a great site run by Reid Horuff who is a student at the Texas Tech University. I was astonished seeing him write a code to solve a sudoku grid within 20 lines.

Applying Obfuscated C Code concepts and some good old recursion, he achieved what programmers would go into orgasms after viewing.

I would like to provide the link for this site so that you could go into his site and watch the maestro's thoughts and works.

I am posting the C code down here so that you may try them out yourself.

-------------------------------------------------------------------------------------------------------------

int g[81][11];

int tile(int x)
{
 int a;
 for(a=1; a<10 && x<81 && !g[x][10]; a++)
  g[x][a] = 1;

 for(a=0; a<9 && x<81 && !g[x][10]; a++)
  g[x][g[x/9*9+a][0]] = g[x][g[x%9+9*a][0]] = g[x][g[x%9/3*3+x/27*27+a/3*9+a%3][0]] = 0;

 for(a=0; a<10 && x<81; a++)
  if(g[x][a])
  {
   g[x][0] = a?a:g[x][10];

   if(tile(x+1))
    return printf(x%9?"%d ":"%d \n",g[x][0])|1;
  }

 g[x][0] = g[x][10];
 return x>80;
}

int main()
{
 int x;
 char c[9];
 for(x=0; x<81 && (x%9 || scanf("%s",c)|1); x++)
  g[80-x][10] = g[80-x][0] = (c[x%9]-48);

 return tile(0);
}
Please check this out.

Adieu

No comments:

Post a Comment