Advertisement
Guest User

8Queens

a guest
Oct 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void comb(int x, int y, int polje[8][8], int coords[12][8][2], int *n); //Ispituje sve mogucnosti za polozaj kraljica
  6. void motionQ(int x, int y, int polje[8][8]); //Ucrtava u polje kuda se kraljica moze kretati
  7. int Num_Of_Queens(int polje[8][8]); //Vraca broj kraljica u polju
  8. void SolExist(int CurCoords [8][2], int coords[12][8][2], int *n); // Provjerava dali rjesenje vec postoji
  9. void rotate90(int coords[8][2]); //Rotira figure za 90
  10. void sort(int coords[8][2]); //Sortira koordinate po retku i stupcu
  11. void mirrorX(int coords[8][2]); //Zrcali figure s obzirom na X os
  12. void mirrorY(int coords[8][2]); //Zrcali figure s obzirom na Y os
  13.  
  14. int main()
  15. {
  16.     int coords[12][8][2];
  17.     int n = 0;
  18.    
  19.     for(int i = 0; i<8; i++)
  20.     {
  21.         for(int j = 0; j<8; j++)
  22.         {
  23.             int polje[8][8] = {};
  24.  
  25.             comb(i, j, polje, coords, &n);
  26.         }
  27.     }
  28.  
  29.     for(int i = 0; i<n; i++)
  30.     {
  31.         int l = 0;
  32.         for(int j = 0; j<8; j++)
  33.         {
  34.             for(int k = 0; k<8; k++)
  35.             {
  36.                 if(coords[i][l][0] == j && coords[i][l][1] == k)
  37.                 {
  38.                     l++;
  39.                     printf("1 ");
  40.                 }
  41.                 else printf("0 ");
  42.             }
  43.             printf("\n");
  44.         }
  45.  
  46.         for(int j = 0; j<8; j++)
  47.         {
  48.             printf("%c %d | ", 65+coords[i][j][1], -1*coords[i][j][0]+8);
  49.         }
  50.         printf("\n\n");
  51.     }
  52.  
  53.     printf("Broj rjesenja: %d", n);
  54.  
  55.     return 0;
  56. }
  57.  
  58. void comb(int x, int y, int polje[8][8], int coords[12][8][2], int *n)
  59. {
  60.     polje[x][y] = 2;
  61.  
  62.     motionQ(x, y, polje);
  63.  
  64.     for(int i = 0; i<8; i++)
  65.     {
  66.         for(int j = 0; j<8; j++)
  67.         {
  68.             if(polje[i][j] == 0)
  69.             {
  70.                 int polje2[8][8] = {};
  71.                 memcpy(polje2, polje, 64*sizeof(int));
  72.                
  73.                 comb(i, j, polje2, coords, n);
  74.                 int NumOfQ = Num_Of_Queens(polje2);
  75.  
  76.                 if(NumOfQ==8)
  77.                 {
  78.                     int CurCoords[8][2];
  79.                     int z = 0;
  80.                     for(int k = 0; k<8; k++)
  81.                     {
  82.                         for(int l = 0; l<8; l++)
  83.                         {
  84.                             if(polje2[k][l] == 2)
  85.                             {
  86.                                 CurCoords[z][0] = k;
  87.                                 CurCoords[z++][1] = l;
  88.                             }
  89.  
  90.                         }
  91.                     }
  92.                     SolExist(CurCoords, coords, n);
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99. void motionQ(int x, int y, int polje[8][8])
  100. {
  101.     for(int i = 0; i<8; i++)
  102.     {
  103.         if(polje[i][y] != 2) polje[i][y] = 1;
  104.         if(polje[x][i] != 2) polje[x][i] = 1;
  105.     }
  106.  
  107.     int i = x-1;
  108.     int j = y-1;
  109.     while(i>=0 && j>=0 && i<=7 && j<=7)
  110.     {
  111.         if(polje[i][j] != 2) polje[i][j]  = 1;
  112.         i = i-1;
  113.         j = j-1;
  114.     }
  115.  
  116.     i = x-1;
  117.     j = y+1;
  118.     while(i>=0 && j>=0 && i<=7 && j<=7)
  119.     {
  120.         if(polje[i][j] != 2) polje[i][j] = 1;
  121.         i = i-1;
  122.         j = j+1;
  123.     }
  124.  
  125.     i = x+1;
  126.     j = y-1;
  127.     while(i>=0 && j>=0 && i<=7 && j<=7)
  128.     {
  129.         if(polje[i][j] != 2) polje[i][j] = 1;
  130.         i = i+1;
  131.         j = j-1;
  132.     }
  133.  
  134.     i = x+1;
  135.     j = y+1;
  136.     while(i>=0 && j>=0 && i<=7 && j<=7)
  137.     {
  138.         if(polje[i][j] != 2) polje[i][j] = 1;
  139.         i = i+1;
  140.         j = j+1;
  141.     }
  142. }
  143.  
  144. int Num_Of_Queens(int polje[8][8])
  145. {
  146.     int count = 0;
  147.     for(int i=0; i<8; i++)
  148.     {
  149.         for(int j=0; j<8; j++)
  150.         {
  151.             if(polje[i][j] == 2) count++;
  152.         }
  153.     }
  154.     return count;
  155. }
  156.  
  157. void SolExist(int CurCoords[8][2], int coords[12][8][2], int *n)
  158. {
  159.     int b;
  160.     if(n>0)
  161.     {
  162.         for(int i = 0; i<*n; i++)
  163.         {
  164.             for(int k = 0; k<4; k++)
  165.             {  
  166.                 if(k>0) rotate90(CurCoords);
  167.                 b = 0;
  168.                 for(int j=0; j<8; j++)
  169.                 {
  170.                     if(coords[i][j][0] == CurCoords[j][0] && coords[i][j][1] == CurCoords[j][1]) b++;
  171.                 }
  172.                 if(b==8) break;
  173.  
  174.                 b=0;
  175.                 mirrorX(CurCoords);
  176.                 for(int j=0; j<8; j++)
  177.                 {
  178.                     if(coords[i][j][0] == CurCoords[j][0] && coords[i][j][1] == CurCoords[j][1]) b++;
  179.                 }
  180.                 mirrorX(CurCoords);
  181.                 if(b==8) break;
  182.  
  183.                 b=0;
  184.                 mirrorY(CurCoords);
  185.                 for(int j=0; j<8; j++)
  186.                 {
  187.                     if(coords[i][j][0] == CurCoords[j][0] && coords[i][j][1] == CurCoords[j][1]) b++;
  188.                 }
  189.                 mirrorY(CurCoords);
  190.                 if(b==8) break;
  191.             }
  192.  
  193.             if(b==8) break;    
  194.         }
  195.     }
  196.  
  197.     if(b!=8 || *n == 0)
  198.     {
  199.         for(int i = 0; i<8; i++)
  200.         {
  201.             coords[*n][i][0] = CurCoords[i][0];
  202.             coords[*n][i][1] = CurCoords[i][1];
  203.         }
  204.         (*n)++;
  205.     }  
  206. }
  207.  
  208. void rotate90(int coords[8][2])
  209. {
  210.     for(int i = 0; i<8; i++)
  211.     {
  212.         int x = coords[i][0];
  213.         coords[i][0] = coords[i][1];
  214.         coords[i][1] = 7-x;
  215.     }
  216.     sort(coords);
  217. }
  218.  
  219. void sort(int coords[8][2])
  220. {
  221.     for (int i = 1; i < 8; i++)
  222.     {
  223.         int tmp[2] = {coords[i][0], coords[i][1]};
  224.  
  225.         int j = i;
  226.         while (j > 0 && coords[j - 1][0] > tmp[0])
  227.         {
  228.             coords[j][0] = coords[j - 1][0];
  229.             coords[j][1] = coords[j - 1][1];
  230.             j--;
  231.         }
  232.         coords[j][0] = tmp[0];
  233.         coords[j][1] = tmp[1];
  234.     }
  235. }
  236.  
  237. void mirrorX(int coords[8][2])
  238. {
  239.     for(int i = 0; i<8; i++)
  240.     {
  241.         coords[i][1]= 7-coords[i][1];
  242.     }
  243.     sort(coords);
  244. }
  245.  
  246. void mirrorY(int coords[8][2])
  247. {
  248.     for(int i = 0; i<8; i++)
  249.     {
  250.         coords[i][0]= 7-coords[i][0];
  251.     }
  252.     sort(coords);
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement