Advertisement
heroys6

FAX Lab 4 8_Queens

Oct 20th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. #define Q "Q"
  7. #define Fil " "
  8.  
  9. struct cell {
  10.     int x, y;
  11.     int imp;
  12.     cell() { x = -1; y = -1; imp = 0; }
  13. };
  14.  
  15. cell toDraw[8];
  16.  
  17. inline bool draw_now(int x, int y)
  18. {
  19.     for (int i = 0; i < 8; i++)
  20.     if (toDraw[i].x == x && toDraw[i].y == y) return true;
  21.     return false;
  22. }
  23.  
  24. void draw_board()
  25. {
  26.     int funcX = 0, funcY = 0;
  27.  
  28.     // Top board edge
  29.     cout << "\n\t";
  30.  
  31.     cout << (char)218;
  32.     for (int i = 0; i < 7; i++)
  33.         cout << (char)196 << (char)194;
  34.     cout << (char)196 << (char)191;
  35.  
  36.     cout << "\n\t";
  37.  
  38.     for (int i = 0; i < 7; i++) {
  39.         // Base of every string
  40.         cout << (char)179;
  41.         for (int i = 0; i < 7; i++) {
  42.             if (draw_now(funcX, funcY)) cout << Q;
  43.             else cout << Fil;
  44.             cout << (char)179;
  45.             funcX++;
  46.         }
  47.         if (draw_now(funcX, funcY)) cout << Q;
  48.         else cout << Fil;
  49.         cout << (char)179;
  50.         funcX = 0;
  51.  
  52.         cout << "\n\t";
  53.  
  54.         cout << (char)195;
  55.         for (int i = 0; i < 7; i++)
  56.             cout << (char)196 << (char)197;
  57.         cout << (char)196 << (char)180;
  58.  
  59.         cout << "\n\t";
  60.         funcY++;
  61.     }
  62.  
  63.     // Last string
  64.     cout << (char)179;
  65.     for (int i = 0; i < 7; i++) {
  66.         if (draw_now(funcX, funcY)) cout << Q;
  67.         else cout << Fil;
  68.         cout << (char)179;
  69.         funcX++;
  70.     }
  71.     if (draw_now(funcX, funcY)) cout << Q;
  72.     else cout << Fil;
  73.     cout << (char)179;
  74.  
  75.     cout << "\n\t";
  76.  
  77.     // Bottom board edge
  78.     cout << (char)192;
  79.     for (int i = 0; i < 7; i++)
  80.         cout << (char)196 << (char)193;
  81.     cout << (char)196 << (char)217;
  82.  
  83.     cout << "\n";
  84. }
  85.  
  86. bool isAllowed(const cell &forCh)
  87. {
  88.     int x = forCh.x, y = forCh.y;
  89.     // Up
  90.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  91.     if (draw_now(x, y++)) return false;
  92.  
  93.     x = forCh.x;
  94.     y = forCh.y;
  95.  
  96.     // Down
  97.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  98.     if (draw_now(x, y--)) return false;
  99.  
  100.     x = forCh.x;
  101.     y = forCh.y;
  102.  
  103.     // Left
  104.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  105.     if (draw_now(x--, y)) return false;
  106.  
  107.     x = forCh.x;
  108.     y = forCh.y;
  109.  
  110.     // Right
  111.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  112.     if (draw_now(x++, y)) return false;
  113.  
  114.     x = forCh.x;
  115.     y = forCh.y;
  116.  
  117.     // Diag. left & up
  118.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  119.     if (draw_now(x--, y++)) return false;
  120.  
  121.     x = forCh.x;
  122.     y = forCh.y;
  123.  
  124.     // Diag. right & up
  125.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  126.     if (draw_now(x++, y++)) return false;
  127.  
  128.     x = forCh.x;
  129.     y = forCh.y;
  130.  
  131.     // Diag. left & down
  132.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  133.     if (draw_now(x--, y--)) return false;
  134.  
  135.     x = forCh.x;
  136.     y = forCh.y;
  137.  
  138.     // Diag. right & down
  139.     while (x >= 0 && x < 8 && y >= 0 && y < 8)
  140.     if (draw_now(x++, y--)) return false;
  141.  
  142.     return true;
  143. }
  144.  
  145. int ansLine = 0;
  146.  
  147. class answers {
  148.     cell ans[92][8];
  149. public:
  150.     answers() {
  151.         for (int i = 0; i < 92; i++)
  152.         for (int j = 0; j < 8; j++) {
  153.             ans[i][j].x = -1;
  154.             ans[i][j].y = -1;
  155.             ans[i][j].imp = 0;
  156.         }
  157.     }
  158.     void push_ans(cell *ptr) {
  159.         bool coinc = false;
  160.  
  161.         for (int i = 0; i < ansLine; i++) {
  162.             for (int m = 0; m < 8; m++) {
  163.                 if (ptr[m].x == ans[i][m].x && ptr[m].y == ans[i][m].y)
  164.                     coinc = true;
  165.                 else {
  166.                     coinc = false;
  167.                     break;
  168.                 }
  169.             }
  170.             if (coinc) break;
  171.         }
  172.         if (!coinc) {
  173.             for (int n = 0; n < 8; n++) {
  174.                 ans[ansLine][n].x = ptr[n].x;
  175.                 ans[ansLine][n].y = ptr[n].y;
  176.             }
  177.             ansLine++; // max == 91(ansLine == 92)
  178.         }
  179.     }
  180.     void print_ans() {
  181.         if (!ansLine) {
  182.             cout << "\nNo results!\n";
  183.             return;
  184.         }
  185.         for (int i = 0; i < ansLine; i++) {
  186.             for (int j = 0; j < 8; j++) {
  187.                 toDraw[j].x = ans[i][j].x;
  188.                 toDraw[j].y = ans[i][j].y;
  189.             }
  190.             if (ansLine) draw_board();
  191.         }
  192.  
  193.     }
  194. } ansList;
  195.  
  196. int main()
  197. {
  198.     cell board[8][8];
  199.  
  200.     // Add information about every cell
  201.     for (int i = 0; i < 8; i++)
  202.     for (int j = 0; j < 8; j++) {
  203.         board[i][j].x = j;
  204.         board[i][j].y = i;
  205.         board[i][j].imp += 15;
  206.         i <= j ? board[i][j].imp += i : board[i][j].imp += j;
  207.         (7 - i) <= j ? board[i][j].imp += (7 - i) : board[i][j].imp += j;
  208.         (7 - i) <= (7 - j) ? board[i][j].imp += (7 - i) : board[i][j].imp += (7 - j);
  209.         i <= (7 - j) ? board[i][j].imp += i : board[i][j].imp += (7 - j);
  210.     }
  211.  
  212.     int impMin = 22, solutions = 0, drwIndx = 0;
  213.     int startPosA = 0, startPosB = 0, startPosC = 0, startPosD = 0,
  214.         startPosE = 0, startPosF = 0, startPosG = 0;
  215.     register int i = 0, j = 0;
  216.     double startCl, endCl;
  217.  
  218.     cout << "Search for solution...\n";
  219.  
  220.     startCl = clock();
  221.     while (startPosG < 8) {
  222.         while (startPosF < 8) {
  223.             while (startPosE < 8) {
  224.                 while (startPosD < 8) {
  225.                     while (startPosC < 8) {
  226.                         while (startPosB < 8) {
  227.                             while (startPosA < 8) {
  228.                                 for (i = 0; i < 8; i++) { // Bust starts here
  229.                                     while (j < 8) {
  230.                                         if (i == 0 && j == 0) j = startPosA;
  231.                                         if (i == 1 && j == 0) j = startPosB;
  232.                                         if (i == 2 && j == 0) j = startPosC;
  233.                                         if (i == 3 && j == 0) j = startPosD;
  234.                                         if (i == 4 && j == 0) j = startPosE;
  235.                                         if (i == 5 && j == 0) j = startPosF;
  236.                                         if (i == 6 && j == 0) j = startPosG;
  237.                                         //if (board[i][j].imp == 22) {
  238.                                         if (isAllowed(board[i][j])) {
  239.                                             toDraw[drwIndx++] = board[i][j];
  240.                                             continue;
  241.                                         }
  242.                                         //}
  243.                                         j++;
  244.                                     }
  245.                                     j = 0;
  246.                                 }
  247.                                 if (drwIndx == 8) { // How many results must be found
  248.                                     ansList.push_ans(toDraw);
  249.                                     solutions++;
  250.                                     /*cout << solutions << ") ";
  251.                                     for (int i = 0; i < 8; i++) {
  252.                                     cout << "[" << toDraw[i].x << ", " << toDraw[i].y << "]  ";
  253.                                     }
  254.                                     cout << "\n\n";*/
  255.                                 }
  256.                                 for (int p = 0; p < 8; p++) {
  257.                                     toDraw[p].x = -1;
  258.                                     toDraw[p].y = -1;
  259.                                     toDraw[p].imp = 0;
  260.                                 }
  261.                                 drwIndx = 0;
  262.                                 startPosA++;
  263.                             }
  264.                             startPosA = 0;
  265.                             startPosB++;
  266.                         }
  267.                         startPosA = 0;
  268.                         startPosB = 0;
  269.                         startPosC++;
  270.                     }
  271.                     startPosA = 0;
  272.                     startPosB = 0;
  273.                     startPosC = 0;
  274.                     startPosD++;
  275.                 }
  276.                 startPosA = 0;
  277.                 startPosB = 0;
  278.                 startPosC = 0;
  279.                 startPosD = 0;
  280.                 startPosE++;
  281.             }
  282.             startPosA = 0;
  283.             startPosB = 0;
  284.             startPosC = 0;
  285.             startPosD = 0;
  286.             startPosE = 0;
  287.             startPosF++;
  288.         }
  289.         startPosA = 0;
  290.         startPosB = 0;
  291.         startPosC = 0;
  292.         startPosD = 0;
  293.         startPosE = 0;
  294.         startPosF = 0;
  295.         startPosG++;
  296.     }
  297.     endCl = clock();
  298.  
  299.     ansList.print_ans();
  300.     cout << "\nFound " << solutions << " solutions\nEstimated " << (endCl - startCl) / CLOCKS_PER_SEC << " seconds\n\n";
  301.     cout << "Unique placement: " << ansLine << "\n\n";
  302.  
  303.     system("pause");
  304.     return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement