Advertisement
Guest User

Sudoku Solver

a guest
Jul 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. Header...
  2.  
  3. #ifndef FT_HEADER_H
  4. # define FT_HEADER_H
  5.  
  6. int     ft_check_column(char **sudoku, int x, char nr);
  7. int     ft_check_row(char **sudoku, int, char);
  8. int     ft_check_square(char **sudoku, int x, int y, char nr);
  9. int     ft_solve_sudoku(char **sudoku, int, int);
  10. int     ft_check_collision(char **sudoku, int x, int y, char nr);
  11. void    ft_display_sudoku(char **sudoku);
  12.  
  13. #endif
  14.  
  15. Functie 1, ft_check_row
  16.  
  17. #include "ft_header.h"
  18.  
  19. int     ft_check_row(char **sudoku, int y, char nr)
  20. {
  21.     int rowc;
  22.  
  23.     rowc = 0;
  24.     while (rowc < 9)
  25.     {
  26.         if (sudoku[y][rowc] == nr)
  27.             return (1);
  28.         rowc += 1;
  29.     }
  30.     return (0);
  31. }
  32.  
  33. functie 2, ft_check_column
  34.  
  35. #include "ft_header.h"
  36.  
  37. int     ft_check_column(char **sudoku, int x, char nr)
  38. {
  39.     int colc;
  40.  
  41.     colc = 0;
  42.     while (colc < 9)
  43.     {
  44.         if (sudoku[colc][x] == nr)
  45.             return (1);
  46.         colc += 1;
  47.     }
  48.     return (0);
  49. }
  50.  
  51. Functie 3; ft_check_square
  52.  
  53. #include "ft_header.h"
  54.  
  55. int     ft_check_square(char **sudoku, int x, int y, char nr)
  56. {
  57.     int w;
  58.     int l;
  59.  
  60.     x = x - x % 3;
  61.     y = y - y % 3;
  62.     w = x;
  63.     l = y;
  64.     while (l < (x + 3))
  65.     {
  66.         while (w < (y + 3))
  67.         {
  68.             if (sudoku[l][w] == nr)
  69.                 return (1);
  70.             w += 1;
  71.         }
  72.         l += 1;
  73.     }
  74.     return (0);
  75. }
  76.  
  77. Functie 4; ft_solve_sudoku
  78.  
  79. #include <unistd.h>
  80. #include "ft_header.h"
  81.  
  82. int     ft_solve_sudoku(char **sudoku, int x, int y)
  83. {
  84.     char    nr;
  85.     int     tx;
  86.     int     ty;
  87.  
  88.     tx = 0;
  89.     ty = 0;
  90.     nr = '1';
  91.     if ((sudoku[y][x] != '0') && (x == 8 && y == 8))
  92.         return (1);
  93.     if (x <= 8)
  94.         x += 1;
  95.     if (y == 9)
  96.     {
  97.         x = 0;
  98.         y += 1;
  99.     }
  100.     if (ft_solve_sudoku(sudoku, x, y))
  101.         return (1);
  102.     if (sudoku[y][x] == '0')
  103.     {
  104.         while (nr <= '9')
  105.         {
  106.             if (ft_check_collision(sudoku, x, y, nr) != 1)
  107.             {
  108.                 sudoku[y][x] = nr;
  109.                 if (x == 8 && y == 8)
  110.                     return (1);
  111.                 if (x <= 8)
  112.                     tx = x + 1;
  113.                 else if (y <= 8)
  114.                 {
  115.                     tx = 0;
  116.                     ty = y + 1;
  117.                 }
  118.                 if (ft_solve_sudoku(sudoku, tx, ty))
  119.                     return (1);
  120.             }
  121.             nr += 1;
  122.         }
  123.         sudoku[y][x] = '0';
  124.     }
  125.     return (0);
  126. }
  127.  
  128. void    ft_display_sudoku(char **sudoku)
  129. {
  130.     int l;
  131.     int w;
  132.  
  133.     l = 0;
  134.     w = 0;
  135.     while (l < 9)
  136.     {
  137.         while (w < 9)
  138.         {
  139.             write(1, &sudoku[l][w], 1);
  140.             w += 1;
  141.         }
  142.         l += 1;
  143.     }
  144. }
  145.  
  146. Functie 5; main
  147.  
  148. #include "ft_header.h"
  149. #include <stdlib.h>
  150. #include <unistd.h>
  151.  
  152. int     ft_strlen(char *str)
  153. {
  154.     int i;
  155.  
  156.     i = 0;
  157.     while (str[i] != '\0')
  158.         i++;
  159.     return (i);
  160. }
  161.  
  162. char    **make_valid(char **str)
  163. {
  164.     int     x;
  165.     int     y;
  166.     char    **sudoku;
  167.  
  168.     sudoku = (char **)malloc(sizeof(char *) * 9 + 1);
  169.     y = 0;
  170.     while (y < 9)
  171.     {
  172.         if (ft_strlen(str[y]) != 9)
  173.             return (0);
  174.         sudoku[y] = (char *)malloc(sizeof(char) * 9 + 1);
  175.         x = 0;
  176.         while (x < 9)
  177.         {
  178.             if (str[y][x] == '.')
  179.                 sudoku[y][x] = 0;
  180.             else if (str[y][x] >= '1' && str[y][x] <= '9')
  181.                 sudoku[y][x] = str[y][x] - '0';
  182.             else
  183.                 return (0);
  184.             x++;
  185.         }
  186.         y++;
  187.     }
  188.     return (sudoku);
  189. }
  190.  
  191. int     main(int arg, char **str)
  192. {
  193.     char **nrs;
  194.  
  195.     if (arg != 10)
  196.     {
  197.         write(1, "Error\n", 6);
  198.         return (0);
  199.     }
  200.     nrs = make_valid(&str[1]);
  201.     ft_solve_sudoku(nrs, 0, 0);
  202.     if (nrs == 0)
  203.         write(1, "Error\n", 6);
  204.     ft_display_sudoku(nrs);
  205.     return (0);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement