eg0rmaffin

sudoku

Jul 14th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   sudoky.c                                           :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: squiana <[email protected]>                     +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2019/07/14 20:46:01 by squiana           #+#    #+#             */
  9. /*   Updated: 2019/07/14 21:17:56 by ckumera          ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <unistd.h>
  14.  
  15. int     test_line(char **tab, char nb, int i);
  16. int     test_column(char **tab, char nb, int j);
  17. int     test_block(char **tab, int i, int j, char nb);
  18.  
  19. void    ft_putchar(char c)
  20. {
  21.     write(1, &c, 1);
  22. }
  23.  
  24. void    display(char **tab)
  25. {
  26.     int i;
  27.     int j;
  28.  
  29.     i = 1;
  30.     while (i <= 9)
  31.     {
  32.         j = 0;
  33.         while (tab[i][j] != '\0')
  34.         {
  35.             ft_putchar(tab[i][j]);
  36.             if (j != 8)
  37.                 ft_putchar(' ');
  38.             j++;
  39.         }
  40.         ft_putchar('\n');
  41.         i++;
  42.     }
  43. }
  44. int sudoky(int place, char **tab)
  45. {
  46.     int     i;
  47.     int     j;
  48.     char    nb;
  49.  
  50.     nb = '0';
  51.     i = place / 9;
  52.     j = place % 9;
  53.     if (place == 90)
  54.         return (1);
  55.     if (tab[i][j] != '.')
  56.         return (sudoky(place + 1, tab));
  57.     while (++nb <= '9')
  58.     {
  59.         if (test_line(tab, nb, i) + test_column(tab, nb, j) + test_block(tab, i, j, nb) == 3)
  60.         {
  61.             tab[i][j] = nb;
  62.             if (sudoky(place + 1, tab))
  63.                 return (1);
  64.         }
  65.     }
  66.     tab[i][j] = '.';
  67.     return (0);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment