Advertisement
Guest User

Whole code

a guest
Dec 21st, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int check(int array[8][8])
  4. {
  5.     int i, j, n, sum;
  6.     int rowsum[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  7.     int colsum[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  8.     int diagsum_l2r[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  9.     int diagsum_r2l[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  10.  
  11.     for (i = 0; i <= 7; i++) //test if are 2 queens on the same row (i: row, j: column)
  12.     {                      
  13.         for (j = 0; j <= 7; j++)
  14.         {
  15.             rowsum[i] += array[i][j]; /*since they are represented by 0s and 1s,
  16.                                     if a row's sum is bigger than 1
  17.                                     there is more than 1 queen on that particular row
  18.                                     here the row doesn't change until all
  19.                                     columns are accessed (we get a row sum)*/
  20.         }
  21.     }
  22.  
  23.  
  24.     for (i = 0; i <= 7; i++) //same as before, but for columns
  25.     {                      
  26.         for (j = 0; j <= 7; j++)
  27.         {
  28.             colsum[i] += array[j][i]; //here the col. doesn't change until all rows are accessed (we get a col. sum)
  29.         }
  30.     }
  31.  
  32. //IMPORTANT!! Diagonal sum code starts here <this is the code for the "\" (left-to-right) diagonals>
  33.     for (n = 0; n <= 7; n++) //n counts the squares of each diagonal
  34.     {
  35.         sum = 0; //initialize sum to 0, here goes the sum of each diagonal line
  36.         for (i = 0; i <= n; i++)
  37.         {
  38.             for (j = 0; j <= n; j++)                                    //What we basically do here is this:
  39.             {                                               //We start from the top left corner. Then,
  40.                 if (i + j == n) //if element belongs to the main diagonal of the "sub-board"        //following the major "\" diagonal line of the main board,
  41.                 {//Observe: For all elements: i + j = n                         //we divide the board into "sub-boards" of size n.
  42.                     sum += array[i][j]; //then add it to the general sum                //The desired sum is each time the sum of the sub-board's "/" major diagonal.
  43.                 }
  44.             }
  45.         }
  46.         diagsum_l2r[n] = sum; //the sum of each diagonal goes to the array
  47.     }
  48.  
  49.     for (n = 0; n <= 6; n++)  //same for the rest 7 diagonals
  50.     {
  51.         sum = 0;
  52.         for (i = 7; i >= 0; i--)
  53.         {
  54.             for (j = 7; j >= 0; j--)
  55.             {
  56.                 if (i + j == 14 - n)
  57.                 {
  58.                     sum += array[i][j];
  59.                 }
  60.             }
  61.         }
  62.         diagsum_l2r[14 - n] = sum;
  63.     }
  64.  
  65.     for (n = 0; n <= 7; n++) //first 8 "/" diagonals (starting from the top left corner)
  66.     {
  67.         sum = 0;
  68.         for (i = 7; i >= 7 - n; i--)
  69.         {
  70.             for (j = 0; j <= n; j++)
  71.             {
  72.                 if (i - j == 7 - n)
  73.                 {
  74.                     sum += array[i][j];
  75.                 }
  76.             }
  77.         }
  78.         diagsum_r2l[n] = sum;
  79.     }
  80.  
  81.     for (n = 0; n <= 6; n++) //last 7 "/" diagonals
  82.     {
  83.         sum = 0;
  84.         for (i = 0; i <= 7; i++)
  85.         {
  86.             for (j = 7; j >= 0; j--)
  87.             {
  88.                 if (j - i == 7 - n)
  89.                 {
  90.                     sum += array[i][j];
  91.                 }
  92.             }
  93.         }
  94.         diagsum_r2l[14 - n] = sum;
  95.     }
  96.    
  97.     for (i = 0; i <= 7; i++)
  98.     {
  99.         if (rowsum[i] > 1 || colsum[i] > 1)
  100.         {
  101.             return 1;
  102.         }
  103.     }
  104.    
  105.     for (j = 0; j <= 14; j++)
  106.     {
  107.         if (diagsum_l2r[j] > 1 || diagsum_r2l[j] > 1)
  108.         {
  109.             return 1;
  110.         }
  111.     }
  112.    
  113.     return 0;
  114. }
  115.  
  116.  
  117. int main(void)
  118. {
  119.     printf("\n8 Queens Problem - Solution Validity Checker by lightspot21\n\n");
  120.     int i = 1; //counter for the input
  121.     int j, k, result; //counters for array printout
  122.     int row = 0;
  123.     int column = 0; //row and column numbers
  124.  
  125.     int board[8][8] = {
  126.         {0, 0, 0, 0, 0, 0, 0, 0},   //here we initialize an empty board as an 8x8 array
  127.         {0, 0, 0, 0, 0, 0, 0, 0},   //from now on: a 0 is an empty square, a 1 is a queen
  128.         {0, 0, 0, 0, 0, 0, 0, 0},
  129.         {0, 0, 0, 0, 0, 0, 0, 0},
  130.         {0, 0, 0, 0, 0, 0, 0, 0},
  131.         {0, 0, 0, 0, 0, 0, 0, 0},
  132.         {0, 0, 0, 0, 0, 0, 0, 0},
  133.         {0, 0, 0, 0, 0, 0, 0, 0}
  134.     };
  135.    
  136.     while (i <= 8) //we fill our board with queens
  137.     {
  138.         printf("Queen #%d row:", i);
  139.         scanf("%d", &row);
  140.         printf("Queen #%d column:", i);
  141.         scanf("%d", &column);
  142.         board[row - 1][column - 1] = 1;
  143.         i++;
  144.     }
  145.    
  146.     printf("\nYour input is as follows: (0 = empty, 1 = queen)\n\n");
  147.     for (j = 0; j <= 7; j++) //prints the board as in a real chessboard (first row at bottom)
  148.     {
  149.         printf("%d - ", j + 1);
  150.         for (k = 0; k <= 7; k++)
  151.         {
  152.             printf("%d ", board[7 - j][k]);
  153.         }
  154.         printf("\n");
  155.     }
  156.     printf("    | | | | | | | |\n");
  157.     printf("    a b c d e f g h\n");
  158.  
  159.     result = check(board);
  160.     if (result == 1)
  161.     {
  162.         printf("\nThe input is not a valid solution to the 8 Queens problem.\n");
  163.     }
  164.     else
  165.     {
  166.         printf("\nThe input is a valid solution to the 8 Queens problem.\n");
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement