Advertisement
evandrix

SudokuSolver.c

Nov 17th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int g[81][11];
  4.  
  5. int tile(int x)
  6. {
  7.     int a;
  8.     for(a=1; a<10 && x<81 && !g[x][10]; a++)
  9.         g[x][a] = 1;
  10.  
  11.     for(a=0; a<9 && x<81 && !g[x][10]; a++)
  12.         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;
  13.  
  14.     for(a=0; a<10 && x<81; a++)
  15.         if(g[x][a])
  16.         {
  17.             g[x][0] = a?a:g[x][10];
  18.  
  19.             if(tile(x+1))
  20.                 return printf(x%9?"%d ":"%d \n",g[x][0])|1;
  21.         }
  22.  
  23.     g[x][0] = g[x][10];
  24.     return x>80;
  25. }
  26.  
  27. int main()
  28. {
  29.     int x;
  30.     char c[9];
  31.     for(x=0; x<81 && (x%9 || scanf("%s",c)|1); x++)
  32.         g[80-x][10] = g[80-x][0] = (c[x%9]-48);
  33.  
  34.     return tile(0);
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement