Advertisement
liftchampion

day 04 ex 09

Oct 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.30 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                             Online C Compiler.
  4.                 Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include<stdio.h>
  10.  
  11. void ft_print_matrix(int m[][8], int size)
  12. {
  13.     int i;
  14.     int j;
  15.    
  16.     i = 0;
  17.     while (i < size)
  18.     {
  19.         j = 0;
  20.         while (j < size)
  21.         {
  22.             printf("%d  ", m[i][j]);
  23.             j++;
  24.         }
  25.         printf("\n");
  26.         i++;
  27.     }
  28. }
  29.  
  30. void ft_fill_matrix(int m[][8], int size)
  31. {
  32.     int i;
  33.     int j;
  34.    
  35.     i = 0;
  36.     while (i < size)
  37.     {
  38.         j = 0;
  39.         while (j < size)
  40.         {
  41.             m[i][j] = 0;
  42.             j++;
  43.         }
  44.         i++;
  45.     }
  46. }
  47.  
  48. /*int ft_set_queen(int m[][8], int x, int y)
  49. {
  50.     m[x][y] = 1;
  51. }
  52.  
  53. int ft_check_line(int m[][8], int x, int y, char dir)
  54. {
  55.     int sum;
  56.     int i;
  57.    
  58.     sum = 0;
  59.     i = 0;
  60.     while (i < 8)
  61.     {
  62.         if (dir == 'H')
  63.         {
  64.             sum += m[x][i];
  65.         }
  66.         if (dir == 'V')
  67.         {
  68.             sum += m[i][y];
  69.         }
  70.         i++;
  71.     }
  72.     return sum;
  73. }
  74.  
  75. char ft_check_diag(char m[][8], char x, char y, int dir)
  76. {
  77.     int sum;
  78.     int i;
  79.     int b;
  80.    
  81.     i = 0;
  82.     sum = 0;
  83.     b = y - dir * x;
  84.     while (i < 8)
  85.     {
  86.         if (dir * i + b >= 0 && dir * i + b < 8)
  87.         {
  88.             sum += m[i][dir * i + b];
  89.             //printf("## %d\n", sum);
  90.         }
  91.        
  92.         i++;
  93.     }
  94.    
  95.     //printf("### %d\n", sum);
  96.    
  97.    
  98.     return sum;
  99. }
  100.  
  101.  
  102.  
  103. char ft_check_queen(char m[][8], char x, char y)
  104. {
  105.     char l;
  106.     char d;
  107.    
  108.     l = ft_check_line(m, x, y, 'H') + ft_check_line(m, x, y, 'V');
  109.     d = ft_check_diag(m, x, y, -1) + ft_check_diag(m, x, y, 1);
  110.     return (l + d + m[x][y]);
  111. }
  112.  
  113. void ft_distribute_queens(char m[][8], char deep, int *vars)
  114. {
  115.     char i = 0;
  116.     char j;
  117.     char a;
  118.     char b;
  119.     char m_c[8][8];
  120.    
  121.     //printf("\nstart deep = %d\n", deep);
  122.     //ft_print_matrix(m, 8);
  123.     //printf("\n\n");
  124.    
  125.     if (deep == 8)
  126.     {
  127.         ft_print_matrix(m, 8);
  128.         printf("\n\n");
  129.         (*vars)++;
  130.         printf("\n########\n");
  131.         return;
  132.     }
  133.    
  134.     while (i++ < 8)
  135.     {
  136.         j = 0;
  137.         while (j++ < 8)
  138.         {
  139.             if (ft_check_queen(m, i - 1, j - 1) == 0)
  140.             {
  141.                 a = 0;
  142.                 while(a++ < 8)
  143.                 {
  144.                     b = 0;
  145.                     while(b++ < 8)
  146.                         m_c[a - 1][b - 1] = m[a - 1][b - 1];
  147.                 }
  148.                 m_c[i - 1][j - 1]++;
  149.                 ft_distribute_queens(m_c, deep + 1, vars);
  150.             }
  151.         }
  152.     }
  153.     //printf("\nend deep = %d\n", deep);
  154.     //ft_print_matrix(m, 8);
  155. }
  156.  
  157. void ft_copy(char m[][8], char m_c[][8])
  158. {
  159.     char i = 0;
  160.     char j = 0;
  161.     while(i++ < 8)
  162.     {
  163.         j = 0;
  164.         while(j++ < 8)
  165.             m_c[i - 1][j - 1] = m[i - 1][j - 1];
  166.     }
  167. }*/
  168.  
  169. int ft_check_diag(int *data, int x, int y)
  170. {
  171.     int i;
  172.     int j;
  173.     int a;
  174.     int b;
  175.    
  176.     i = 0;
  177.     j = 0;
  178.     while (i < 8)
  179.     {
  180.         if (data[i] != 9)
  181.         {
  182.             if (x >= i)
  183.                 a = x - i;
  184.             else
  185.                 a = i - x;
  186.             if (y >= data[i])
  187.                 b = y - data[i];
  188.             else
  189.                 b = data[i] - y;
  190.             if (a == b)
  191.                 return (0);
  192.         }
  193.         i++;
  194.     }
  195.     return (1);
  196. }
  197.  
  198. int ft_check_queen(int *data, int x, int y)
  199. {
  200.     int i;
  201.    
  202.     i = 0;
  203.     //printf("\n########");
  204.     if (data[x] != 9)
  205.     {
  206.         //printf("\n*** 1\n");
  207.         return (0);
  208.     }
  209.     while (i < 8)
  210.     {
  211.         if (data[i++] == y)
  212.         {
  213.             //printf("\n*** 2\n");
  214.             return (0);
  215.         }
  216.     }
  217.     return ft_check_diag(data, x, y);
  218. }
  219.  
  220. void ft_distribute_queens(int *data, int deep, int *vars)
  221. {
  222.     int i;
  223.     int j;
  224.     int k;
  225.     int data_c[8];
  226.    
  227.     if (deep == 2)
  228.     {
  229.         for (int i = 0; i < 8; i++)
  230.         {
  231.             printf("%d ", data[i]);
  232.         }
  233.         printf("\n\n");
  234.         (*vars)++;
  235.         printf("\n########\n");
  236.         return;
  237.     }
  238.     i = 0;
  239.     while (i++ < 8)
  240.     {
  241.         j = 0;
  242.         while (j++ < 8)
  243.         {
  244.             if (ft_check_queen(data, i - 1, j - 1))
  245.             {
  246.                 k = 0;
  247.                 while (k++ < 8)
  248.                     data_c[k - 1] = data[k - 1];
  249.                 data_c[i - 1] = j - 1;
  250.                 ft_distribute_queens(data_c, deep + 1, vars);
  251.             }
  252.         }
  253.     }
  254. }
  255.  
  256. int main(void)
  257. {
  258.    
  259.     int data[8];
  260.     int vars = 0;
  261.    
  262.     for (int i = 0; i < 8; i++)
  263.     {
  264.         data[i] = 9;
  265.     }
  266.     //data[2] = 3;
  267.     for (int i = 0; i < 8; i++)
  268.     {
  269.         printf("%d ", data[i]);
  270.     }
  271.     printf("\n");
  272.    
  273.     ft_distribute_queens(data, 0, &vars);
  274.     printf("\n%d \n", vars);
  275.    
  276.    
  277.     printf("%d %d %d %d %d", ft_check_queen(data, 0, 3), ft_check_queen(data, 2, 6), ft_check_queen(data, 4, 5), ft_check_queen(data, 5, 0), ft_check_queen(data, 7, 7));
  278.    
  279.  
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement