Advertisement
tampurus

Sudoku Solver C PROJECT

Jan 16th, 2024 (edited)
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.30 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdbool.h>
  3.  
  4. // This below function responsible for checking is assumed input satisfying
  5. // 3 conditions ?
  6. bool IsValid(char board[9][9],int row, int col, char c)
  7. {
  8.     for(int i=0 ; i<9 ; i++)
  9.     {
  10.         // if any row contains the assumed number then return false
  11.         if(board[row][i] == c)
  12.         {
  13.             return false;
  14.         }
  15.         // if any column contains the assumed number then return false
  16.         if(board[i][col] == c)
  17.         {
  18.             return false;
  19.         }
  20.         // if any 3*3 grid or house[3*3] contains the assumed
  21.         // number then we should return false
  22.         if(board[3* (row/3) + i/3][3*(col/3) + i%3] == c)
  23.         {
  24.             return false;
  25.         }
  26.     }
  27.  
  28.     // if we comes to this part means assumed number obeying 3 conditions
  29.     // and we can insert the same into the sudoku to check it more in further recursion calls
  30.     return true;
  31. }
  32.  
  33. // This is a recursilve function will be called until all cells will filled obeying 3 conditions
  34. bool solve(char board[9][9])
  35. {
  36.     // visiting every cell in 9X9 matrix or two dimensional array by using two loops
  37.     for(int i=0; i< 9 ; i++)
  38.     {
  39.         for(int j=0 ; j < 9 ; j ++)
  40.         {
  41.  
  42.             // obviously we won't check if cell is filled with any number
  43.             // Or in sudoku term hint is given
  44.             if(board[i][j] == '.')
  45.             {
  46.  
  47.                 // here we are checking each number from 1..9
  48.                 // if any one of them satisfy we can pass true
  49.                 // and go to next recusion call
  50.                 for(char c='1' ; c<='9' ; c++)
  51.                 {
  52.  
  53.                     /* IsValid function check if assume number will satisfy
  54.                         following three condition :-
  55.  
  56.                         1. All the rows should be filled with numbers(1 – 9) exactly once.
  57.  
  58.                         2. All the columns should be filled with numbers(1 – 9) exactly once.
  59.  
  60.                         3. Each 3×3 submatrix should be filled with numbers(1 – 9) exactly once.
  61.                     */
  62.                     if(IsValid(board,i,j,c) == true)
  63.                     {
  64.                         board[i][j] = c;
  65.                        
  66.                         /* if we got true from IsValid fn that means current value is true
  67.                            for provided hint in sudoku it may get wrong in further recursion calls
  68.  
  69.                            that's why we calling solve function recusilvely for next empty cell while checking
  70.                            value previous filled cells
  71.                         */
  72.                         if(solve(board) == true)
  73.                         {
  74.                             return true;
  75.                         }
  76.                         /* If filled cell got true and then later found it is not suitable with other emply cells
  77.                            than after coming by taking false from solve fn we'll re-empty the cell
  78.                         */
  79.  
  80.                         else
  81.                         {
  82.                             board[i][j] = '.';
  83.                         }
  84.                     }
  85.                 }
  86.  
  87.                 /* it means we have checked 1..9 and none of them is satifying
  88.                    so we should discard previous accecpted cell and should try
  89.                    another value
  90.                 */
  91.                 return false;
  92.             }
  93.         }
  94.     }
  95.  
  96.     // We'll come here only when all empty cells are filled with the
  97.     // their correct element
  98.     return true;
  99. }
  100.  
  101. // this the function main function will call
  102. void SolveSudoku(char board[9][9])
  103. {
  104.     solve(board);
  105. }
  106.  
  107. char board[9][9];
  108.  
  109. int main()
  110. {
  111.  
  112.     freopen("tc2.txt", "r", stdin);
  113.     freopen("otpt.txt", "w", stdout);
  114.  
  115.     // taking input from input file into 9X9 two dimensional array
  116.     for(int i=0 ; i<9 ; i++)
  117.     {
  118.         for(int j=0 ; j<9 ; j++)
  119.         {
  120.             scanf(" %c",&board[i][j]);
  121.         }
  122.     }
  123.  
  124.     // this function does not return anything just fill the emty cells in the input
  125.     SolveSudoku(board);
  126.  
  127.     // after sudoku is solved we print the answer
  128.     for(int i=0 ; i<9 ; i++)
  129.     {
  130.         for(int j=0 ; j<9 ; j++)
  131.         {
  132.             printf("%c ",board[i][j]);
  133.         }
  134.         printf("\n");
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
  140.  
  141. /*
  142. Given a 9×9 incomplete sudoku, solve it such that it becomes valid sudoku. Valid sudoku has the following properties.
  143.  
  144. 1. All the rows should be filled with numbers(1 – 9) exactly once.
  145.  
  146. 2. All the columns should be filled with numbers(1 – 9) exactly once.
  147.  
  148. 3. Each 3×3 submatrix should be filled with numbers(1 – 9) exactly once.
  149. */
  150.  
  151.  
  152.  
  153. tc1
  154. 9 5 7 . 1 3 . 8 4
  155. 4 8 3 . 5 7 1 . 6
  156. . 1 2 . 4 9 5 3 7
  157. 1 7 . 3 . 4 9 . 2
  158. 5 . 4 9 7 . 3 6 .
  159. 3 . 9 5 . 8 7 . 1
  160. 8 4 5 7 9 . 6 1 3
  161. . 9 1 . 3 6 . 7 5
  162. 7 . 6 1 8 5 4 . 9
  163.  
  164. ouput
  165. 9 5 7 6 1 3 2 8 4
  166. 4 8 3 2 5 7 1 9 6
  167. 6 1 2 8 4 9 5 3 7
  168. 1 7 8 3 6 4 9 5 2
  169. 5 2 4 9 7 1 3 6 8
  170. 3 6 9 5 2 8 7 4 1
  171. 8 4 5 7 9 2 6 1 3
  172. 2 9 1 4 3 6 8 7 5
  173. 7 3 6 1 8 5 4 2 9
  174.  
  175. tc2
  176. 5 3 .  . 7 .  . . .
  177. 6 . .  1 9 5  . . .
  178. . 9 8  . . .  . 6 .
  179. 8 . .  . 6 .  . . 3
  180. 4 . .  8 . 3  . . 1
  181. 7 . .  . 2 .  . . 6
  182. . 6 .  . . .  2 8 .
  183. . . .  4 1 9  . . 5
  184. . . .  . 8 .  . 7 9
  185.  
  186. output
  187. 5 3 4 6 7 8 9 1 2
  188. 6 7 2 1 9 5 3 4 8
  189. 1 9 8 3 4 2 5 6 7
  190. 8 5 9 7 6 1 4 2 3
  191. 4 2 6 8 5 3 7 9 1
  192. 7 1 3 9 2 4 8 5 6
  193. 9 6 1 5 3 7 2 8 4
  194. 2 8 7 4 1 9 6 3 5
  195. 3 4 5 2 8 6 1 7 9
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement