Guest User

Untitled

a guest
Sep 1st, 2010
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. int solve(int field[][COLS], int row, int col)
  2. {
  3.     int n, m, i;
  4.  
  5.     if (row*col > 64)       /* solved */
  6.     {
  7. //      output(field);
  8.         return 1;
  9.     }
  10.  
  11.     n = row;
  12.     m = col;
  13.  
  14.     n++;            /* set n, m to index of next cell */
  15.     if (n > 8)
  16.     {
  17.     n = 0;
  18.     m++;
  19.     }
  20.  
  21.     if (field[row][col] == 0)   /* use only empty cells */
  22.     {
  23.         for (i=1; i<=9; i++)    /* search allowed number for the cell */
  24.         {
  25.             if (allowed(field, row, col, i))    /* is i allowed? */
  26.             {
  27.                 field[row][col] = i;            /* fill cell with i */
  28.                 if (solve(field, n, m))         /* continue with next cell */
  29.         {
  30.                 return 1;
  31.         }
  32.                 field[row][col] = 0;            /* reset cell if no solution found */
  33.             }
  34.         }
  35.     }
  36.  
  37.     else    /* if cell is not empty, continue with next cell */
  38.     {
  39.         if (solve(field, n, m))
  40.     {
  41.             return 1;
  42.     }
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment