Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int MAX = 20;
- bool geenaanval (int rij, int stand[ ]) {
- bool veilig = true;
- int hulprij = 1;
- while (veilig && (hulprij < rij)) {
- veilig = ((stand[rij] != stand[hulprij]) &&
- ((stand[rij]-stand[hulprij]) != rij-hulprij) &&
- ((stand[rij]-stand[hulprij]) != hulprij-rij));
- hulprij++;
- }//while
- return veilig;
- }//geenaanval
- bool juistedamekolom (int n, int damerij, int stand[ ]) {
- int rij, eerstedamerij, i, xdame, ydame;
- for (rij = 1; rij <= n; rij++) {
- if (stand[rij] == stand[n+2]){
- eerstedamerij = rij;
- break;
- }//if
- }//for
- //
- //DIAGONALEN CONTROLEREN
- // -misschien dat pion ook invloed heeft op diagonalen? (tweede deel if-statements - lijkt geen verandering te geven)
- ydame = stand[n];
- xdame = stand[n+2];
- for (i = 1; i <= n; i++) {
- if ((xdame+i == stand[ydame+i]) &&
- (xdame+i > 0) && (xdame+i < 9) &&
- (ydame+i > 0) && (ydame+i < 9) &&
- !(xdame+i == stand[n+2] && ydame+i == stand[n+3])){
- return false;
- }//if
- if ((xdame+i == stand[ydame-i]) &&
- (xdame+i > 0) && (xdame+i < 9) &&
- (ydame+i > 0) && (ydame-i < 9) &&
- !(xdame+i == stand[n+2] && ydame-i == stand[n+3])){
- return false;
- }//if
- if ((xdame-i == stand[ydame+i]) &&
- (xdame-i > 0) && (xdame-i < 9) &&
- (ydame+i > 0) && (ydame+i < 9) &&
- !(xdame-i == stand[n+2] && ydame+i == stand[n+3])){
- return false;
- }//if
- if ((xdame-i == stand[ydame-i]) &&
- (xdame-i > 0) && (xdame-i < 9) &&
- (ydame+i > 0) && (ydame-i < 9) &&
- !(xdame-i == stand[n+2] && ydame-i == stand[n+3])){
- return false;
- }//if
- }//for
- if (damerij < n) {
- return false;
- }//if
- if ((eerstedamerij < stand[n+3]) && (damerij > stand[n+3])) {
- return true;
- }//if
- else if ((eerstedamerij > stand[n+3]) && (damerij < stand[n+3])) {
- return true;
- }//else if
- return false;
- }
- bool juistedamerij (int n, int damekolom, int stand[ ]) {
- int eerstedamekolom, pionrij, pionkolom, hulprij, i, xdame, ydame;
- pionkolom = stand[n+2];
- pionrij = stand[n+3];
- eerstedamekolom = stand[pionrij];
- for (hulprij = 1; hulprij <= n; hulprij++) {
- if (damekolom == stand[hulprij]) {
- return false;
- }//if
- }//for
- //
- //DIAGONALEN CONTROLEREN
- // -misschien dat pion ook invloed heeft op diagonalen? (tweede deel if-statements - lijkt geen verandering te geven)
- ydame = stand[n+3];
- xdame = stand[n+1];
- for (i = 1; i <= n; i++) {
- if ((xdame+i == stand[ydame+i]) &&
- (xdame+i > 0) && (xdame+i < 9) &&
- (ydame+i > 0) && (ydame+i < 9) &&
- !(xdame+i == stand[n+2] && ydame+i == stand[n+3])){
- return false;
- }//if
- if ((xdame+i == stand[ydame-i]) &&
- (xdame+i > 0) && (xdame+i < 9) &&
- (ydame+i > 0) && (ydame-i < 9) &&
- !(xdame+i == stand[n+2] && ydame-i == stand[n+3])){
- return false;
- }//if
- if ((xdame-i == stand[ydame+i]) &&
- (xdame-i > 0) && (xdame-i < 9) &&
- (ydame+i > 0) && (ydame+i < 9) &&
- !(xdame-i == stand[n+2] && ydame+i == stand[n+3])){
- return false;
- }//if
- if ((xdame-i == stand[ydame-i]) &&
- (xdame-i > 0) && (xdame-i < 9) &&
- (ydame+i > 0) && (ydame-i < 9) &&
- !(xdame-i == stand[n+2] && ydame-i == stand[n+3])){
- return false;
- }//if
- }//for
- if ((eerstedamekolom < pionkolom) && (damekolom > pionkolom)) {
- return true;
- }//if
- else if ((eerstedamekolom > pionkolom) && (damekolom < pionkolom)) {
- return true;
- }//else if
- return false;
- }
- bool juistepion (int n, int pionrij, int pionkolom, int stand[ ]) {
- bool veilig = false; int hulprij;
- if ((pionrij == 1) || (pionrij == n) ||
- (pionkolom == 1) || (pionkolom == n)) {
- return false; //rand
- }//if
- else if ((pionrij == 2 && pionkolom == 2) ||
- (pionrij == 2 && pionkolom == n-1)) {
- return false; //bovenste hoekvlakken aangrezend aan rand
- }//else if
- else if ((pionrij == n-1 && pionkolom == 2) ||
- (pionrij == n-1 && pionkolom == n-1)) {
- return false; //onderste hoekvlakken aangrezend aan rand
- }//else if
- for (hulprij = 1; hulprij <= n; hulprij++) {
- if (stand[hulprij] == pionkolom) {
- veilig = true;
- }//if
- }//for
- return veilig;
- }//juistepion
- void drukaf (int n, int stand[ ]) {
- for (int i = 1; i < n; i++) {
- cout << stand[i] << " ";
- }//for
- cout << stand[n+2] << " ";
- //cout << "," << stand[n] << " ";
- cout << stand[n+1] << "," << stand[n+3] << " ";
- cout << endl;
- }//drukaf
- void zetdames (int n, int rij, int stand[ ], int & aantal) {
- int kolom, pionkolom, pionrij, damekolom, damerij;
- if (rij == n+3) {
- drukaf (n, stand);
- aantal++;
- }//if
- if (rij == n) {
- for (pionkolom = 2; pionkolom < n; pionkolom++) {
- for (pionrij = 2; pionrij < n; pionrij++) {
- stand[n+2] = pionkolom;
- stand[n+3] = pionrij;
- if (juistepion (n, pionrij, pionkolom, stand)) {
- //cout << "Zet pion" << endl;
- zetdames(n, (rij+1), stand, aantal);
- }//if
- }//for
- }//for
- }//else if
- if (rij == n+1) {
- for (damerij = 1; damerij <= n; damerij++) { //komt in zelfde kolom als pion
- stand[n] = damerij;
- if (juistedamekolom (n, damerij, stand)) {
- //cout << "Zet Dame n" << endl;
- zetdames(n, (rij+1), stand, aantal);
- }//if
- }//for
- }//else if
- else if (rij == n+2) {
- for (damekolom = 1; damekolom <= n; damekolom++) { //komt in zelfde rij als pion
- stand[n+1] = damekolom;
- if (juistedamerij (n, damekolom, stand)) {
- //cout << "Zet Dame n+1" << endl;
- zetdames(n, (rij+1), stand, aantal);
- }//if
- }//for
- }//else if
- if (rij <= n-1) {
- for (kolom = 1; kolom <= n; kolom++) {
- stand[rij] = kolom;
- if (geenaanval (rij, stand)){
- //cout << "Zet Dame " << rij << endl;
- zetdames(n, (rij+1), stand, aantal);
- }//if
- }//for
- }//else if
- }//zetdames
- int main () {
- int stand[MAX]; int grootte; int teller = 0;
- do {
- cout << "Grootte schaakbord ( < " << MAX << " ) .. "; cin >> grootte;
- } while (grootte < 1 || grootte >= MAX);
- zetdames(grootte, 1, stand, teller);
- cout << endl << "Aantal: " << teller << endl << endl;
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment