Guest User

Untitled

a guest
Jun 22nd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SIZE 9
  4.  
  5. //sudoku problem
  6. int matrix[9][9] = {
  7.     {6,5,0,8,7,3,0,9,0},
  8.     {0,0,3,2,5,0,0,0,8},
  9.     {9,8,0,1,0,4,3,5,7},
  10.     {1,0,5,0,0,0,0,0,0},
  11.     {4,0,0,0,0,0,0,0,2},
  12.     {0,0,0,0,0,0,5,0,3},
  13.     {5,7,8,3,0,1,0,2,6},
  14.     {2,0,0,0,4,8,9,0,0},
  15.     {0,9,0,6,2,5,0,8,1}
  16. };
  17.  
  18. //function to print sudoku
  19. void print_sudoku()
  20. {
  21.     int i,j;
  22.     for(i=0;i<SIZE;i++)
  23.     {
  24.         for(j=0;j<SIZE;j++)
  25.         {
  26.             printf("%d\t",matrix[i][j]);
  27.         }
  28.         printf("\n\n");
  29.     }
  30. }
  31.  
  32. //function to check if all cells are assigned or not
  33. //if there is any unassigned cell
  34. //then this function will change the values of
  35. //row and col accordingly
  36. int number_unassigned(int *row, int *col)
  37. {
  38.     int num_unassign = 0;
  39.     int i,j;
  40.     for(i=0;i<SIZE;i++)
  41.     {
  42.         for(j=0;j<SIZE;j++)
  43.         {
  44.             //cell is unassigned
  45.             if(matrix[i][j] == 0)
  46.             {
  47.                 //changing the values of row and col
  48.                 *row = i;
  49.                 *col = j;
  50.                 //there is one or more unassigned cells
  51.                 num_unassign = 1;
  52.                 return num_unassign;
  53.             }
  54.         }
  55.     }
  56.     return num_unassign;
  57. }
  58.  
  59. //function to check if we can put a
  60. //value in a paticular cell or not
  61. int is_safe(int n, int r, int c)
  62. {
  63.     int i,j;
  64.     //checking in row
  65.     for(i=0;i<SIZE;i++)
  66.     {
  67.         //there is a cell with same value
  68.         if(matrix[r][i] == n)
  69.             return 0;
  70.     }
  71.     //checking column
  72.     for(i=0;i<SIZE;i++)
  73.     {
  74.         //there is a cell with the value equal to i
  75.         if(matrix[i][c] == n)
  76.             return 0;
  77.     }
  78.     //checking sub matrix
  79.     int row_start = (r/3)*3;
  80.     int col_start = (c/3)*3;
  81.     for(i=row_start;i<row_start+3;i++)
  82.     {
  83.         for(j=col_start;j<col_start+3;j++)
  84.         {
  85.             if(matrix[i][j]==n)
  86.                 return 0;
  87.         }
  88.     }
  89.     return 1;
  90. }
  91.  
  92. //function to solve sudoku
  93. //using backtracking
  94. int solve_sudoku()
  95. {
  96.     int row;
  97.     int col;
  98.     //if all cells are assigned then the sudoku is already solved
  99.     //pass by reference because number_unassigned will change the values of row and col
  100.     if(number_unassigned(&row, &col) == 0)
  101.         return 1;
  102.     int n,i;
  103.     //number between 1 to 9
  104.     for(i=1;i<=SIZE;i++)
  105.     {
  106.         //if we can assign i to the cell or not
  107.         //the cell is matrix[row][col]
  108.         if(is_safe(i, row, col))
  109.         {
  110.             matrix[row][col] = i;
  111.             //backtracking
  112.             if(solve_sudoku())
  113.                 return 1;
  114.             //if we can't proceed with this solution
  115.             //reassign the cell
  116.             matrix[row][col]=0;
  117.         }
  118.     }
  119.     return 0;
  120. }
  121.  
  122. int main()
  123. {
  124.     if (solve_sudoku())
  125.         print_sudoku();
  126.     else
  127.         printf("No solution\n");
  128.     return 0;
  129. }
Add Comment
Please, Sign In to add comment