blastermak

Untitled

Apr 19th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX = 20;
  6.  
  7. bool geenaanval (int rij, int stand[ ]) {
  8.     bool veilig = true;
  9.     int hulprij = 1;
  10.  
  11.     while (veilig && (hulprij < rij)) {
  12.         veilig = ((stand[rij] != stand[hulprij]) &&
  13.              ((stand[rij]-stand[hulprij]) != rij-hulprij) &&
  14.              ((stand[rij]-stand[hulprij]) != hulprij-rij));
  15.         hulprij++;
  16.     }//while
  17.  
  18.     return veilig;
  19. }//geenaanval
  20.  
  21. bool juistedamekolom (int n, int damerij, int stand[ ]) {
  22.     int rij, eerstedamerij, i, xdame, ydame;
  23.  
  24.     for (rij = 1; rij <= n; rij++) {
  25.         if (stand[rij] == stand[n+2]){
  26.             eerstedamerij = rij;
  27.             break;
  28.         }//if
  29.     }//for
  30.  
  31.     //
  32.     //DIAGONALEN CONTROLEREN
  33.     // -misschien dat pion ook invloed heeft op diagonalen? (tweede deel if-statements - lijkt geen verandering te geven)
  34.  
  35.     ydame = stand[n];
  36.     xdame = stand[n+2];
  37.     for (i = 1; i <= n; i++) {
  38.         if ((xdame+i == stand[ydame+i]) &&
  39.              (xdame+i > 0) && (xdame+i < 9) &&
  40.              (ydame+i > 0) && (ydame+i < 9) &&
  41.              !(xdame+i == stand[n+2] && ydame+i == stand[n+3])){
  42.             return false;
  43.         }//if
  44.         if ((xdame+i == stand[ydame-i]) &&
  45.              (xdame+i > 0) && (xdame+i < 9) &&
  46.              (ydame+i > 0) && (ydame-i < 9) &&
  47.              !(xdame+i == stand[n+2] && ydame-i == stand[n+3])){
  48.             return false;
  49.         }//if
  50.         if ((xdame-i == stand[ydame+i]) &&
  51.              (xdame-i > 0) && (xdame-i < 9) &&
  52.              (ydame+i > 0) && (ydame+i < 9) &&
  53.              !(xdame-i == stand[n+2] && ydame+i == stand[n+3])){
  54.             return false;
  55.         }//if
  56.         if ((xdame-i == stand[ydame-i]) &&
  57.              (xdame-i > 0) && (xdame-i < 9) &&
  58.              (ydame+i > 0) && (ydame-i < 9) &&
  59.              !(xdame-i == stand[n+2] && ydame-i == stand[n+3])){
  60.             return false;
  61.         }//if
  62.     }//for
  63.  
  64.     if (damerij < n) {
  65.         return false;
  66.     }//if
  67.  
  68.     if ((eerstedamerij < stand[n+3]) && (damerij > stand[n+3])) {
  69.         return true;
  70.     }//if
  71.     else if ((eerstedamerij > stand[n+3]) && (damerij < stand[n+3])) {
  72.         return true;
  73.     }//else if
  74.  
  75.     return false;
  76. }
  77.  
  78. bool juistedamerij (int n, int damekolom, int stand[ ]) {
  79.     int eerstedamekolom, pionrij, pionkolom, hulprij, i, xdame, ydame;
  80.  
  81.     pionkolom = stand[n+2];
  82.     pionrij = stand[n+3];
  83.     eerstedamekolom = stand[pionrij];
  84.  
  85.     for (hulprij = 1; hulprij <= n; hulprij++) {
  86.         if (damekolom == stand[hulprij]) {
  87.             return false;
  88.         }//if
  89.     }//for
  90.  
  91.     //
  92.     //DIAGONALEN CONTROLEREN
  93.     // -misschien dat pion ook invloed heeft op diagonalen? (tweede deel if-statements - lijkt geen verandering te geven)
  94.  
  95.     ydame = stand[n+3];
  96.     xdame = stand[n+1];
  97.     for (i = 1; i <= n; i++) {
  98.         if ((xdame+i == stand[ydame+i]) &&
  99.              (xdame+i > 0) && (xdame+i < 9) &&
  100.              (ydame+i > 0) && (ydame+i < 9) &&
  101.              !(xdame+i == stand[n+2] && ydame+i == stand[n+3])){
  102.             return false;
  103.         }//if
  104.         if ((xdame+i == stand[ydame-i]) &&
  105.              (xdame+i > 0) && (xdame+i < 9) &&
  106.              (ydame+i > 0) && (ydame-i < 9) &&
  107.              !(xdame+i == stand[n+2] && ydame-i == stand[n+3])){
  108.             return false;
  109.         }//if
  110.         if ((xdame-i == stand[ydame+i]) &&
  111.              (xdame-i > 0) && (xdame-i < 9) &&
  112.              (ydame+i > 0) && (ydame+i < 9) &&
  113.              !(xdame-i == stand[n+2] && ydame+i == stand[n+3])){
  114.             return false;
  115.         }//if
  116.         if ((xdame-i == stand[ydame-i]) &&
  117.              (xdame-i > 0) && (xdame-i < 9) &&
  118.              (ydame+i > 0) && (ydame-i < 9) &&
  119.              !(xdame-i == stand[n+2] && ydame-i == stand[n+3])){
  120.             return false;
  121.         }//if
  122.     }//for
  123.  
  124.     if ((eerstedamekolom < pionkolom) && (damekolom > pionkolom)) {
  125.         return true;
  126.     }//if
  127.     else if ((eerstedamekolom > pionkolom) && (damekolom < pionkolom)) {
  128.         return true;
  129.     }//else if
  130.  
  131.     return false;
  132. }
  133.  
  134. bool juistepion (int n, int pionrij, int pionkolom, int stand[ ]) {
  135.     bool veilig = false; int hulprij;
  136.  
  137.     if ((pionrij == 1) || (pionrij == n) ||
  138.         (pionkolom == 1) || (pionkolom == n)) {
  139.         return false; //rand
  140.     }//if
  141.     else if ((pionrij == 2 && pionkolom == 2) ||
  142.              (pionrij == 2 && pionkolom == n-1)) {
  143.         return false; //bovenste hoekvlakken aangrezend aan rand
  144.     }//else if
  145.     else if ((pionrij == n-1 && pionkolom == 2) ||
  146.              (pionrij == n-1 && pionkolom == n-1)) {
  147.         return false; //onderste hoekvlakken aangrezend aan rand
  148.     }//else if
  149.  
  150.     for (hulprij = 1; hulprij <= n; hulprij++) {
  151.         if (stand[hulprij] == pionkolom) {
  152.             veilig = true;
  153.         }//if
  154.  
  155.     }//for
  156.  
  157.     return veilig;
  158. }//juistepion
  159.  
  160. void drukaf (int n, int stand[ ]) {
  161.         for (int i = 1; i < n; i++) {
  162.             cout << stand[i] << " ";
  163.         }//for
  164.         cout << stand[n+2] << " ";
  165.         //cout << "," << stand[n] << " ";
  166.         cout << stand[n+1] << "," << stand[n+3] << " ";
  167.         cout << endl;
  168. }//drukaf
  169.  
  170. void zetdames (int n, int rij, int stand[ ], int & aantal) {
  171.     int kolom, pionkolom, pionrij, damekolom, damerij;
  172.  
  173.     if (rij == n+3) {
  174.         drukaf (n, stand);
  175.         aantal++;
  176.     }//if
  177.      if (rij == n) {
  178.         for (pionkolom = 2; pionkolom < n; pionkolom++) {
  179.             for (pionrij = 2; pionrij < n; pionrij++) {
  180.                 stand[n+2] = pionkolom;
  181.                 stand[n+3] = pionrij;
  182.                 if (juistepion (n, pionrij, pionkolom, stand)) {
  183.                     //cout << "Zet pion" << endl;
  184.                     zetdames(n, (rij+1), stand, aantal);
  185.                 }//if
  186.             }//for
  187.         }//for
  188.     }//else if
  189.      if (rij == n+1) {
  190.         for (damerij = 1; damerij <= n; damerij++) { //komt in zelfde kolom als pion
  191.             stand[n] = damerij;
  192.             if (juistedamekolom (n, damerij, stand)) {
  193.                 //cout << "Zet Dame n" << endl;
  194.                 zetdames(n, (rij+1), stand, aantal);
  195.             }//if
  196.         }//for
  197.     }//else if
  198.     else if (rij == n+2) {
  199.         for (damekolom = 1; damekolom <= n; damekolom++) { //komt in zelfde rij als pion
  200.             stand[n+1] = damekolom;
  201.             if (juistedamerij (n, damekolom, stand)) {
  202.                 //cout << "Zet Dame n+1" << endl;
  203.                 zetdames(n, (rij+1), stand, aantal);
  204.             }//if
  205.         }//for
  206.     }//else if
  207.      if (rij <= n-1) {
  208.         for (kolom = 1; kolom <= n; kolom++) {
  209.             stand[rij] = kolom;
  210.             if (geenaanval (rij, stand)){
  211.                 //cout << "Zet Dame " << rij << endl;
  212.                 zetdames(n, (rij+1), stand, aantal);
  213.             }//if
  214.         }//for
  215.     }//else if
  216. }//zetdames
  217.  
  218. int main () {
  219.     int stand[MAX]; int grootte; int teller = 0;
  220.  
  221.     do {
  222.         cout << "Grootte schaakbord ( < " << MAX << " ) .. "; cin >> grootte;
  223.     } while (grootte < 1 || grootte >= MAX);
  224.  
  225.     zetdames(grootte, 1, stand, teller);
  226.     cout << endl << "Aantal: " << teller << endl << endl;
  227.     return 0;
  228. }//main
Advertisement
Add Comment
Please, Sign In to add comment