1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* author: ncomputers.org */
#include<cstdlib>
#include<iostream>
using namespace std;
int main(void){
unsigned const int queens=8;
unsigned int diagonal1[]={1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};//diagonal from down to up
unsigned int diagonal2[]={0,0,0,0,0,0,0,8,0,0,0,0,0,0,0};//diagonal from up to down
unsigned int solution[]={0,1,2,3,4,5,6,7};//x position of each queen
unsigned int x,y;//stores temporally values of queen(x,y);
unsigned int x1,y1;//stores temporally values of queen(x1,y1);
unsigned int h=queens;//eight conflicts
unsigned int iterator;//iterator variable
srand(time(0));//seed random
for(iterator=0;iterator<queens;iterator++){//seed solution
y=rand()%queens;//select a queen randomly
do{
y1=rand()%queens;//select other queen randomly
}while(y==y1);//y won't be equal to y1
x=solution[y];//store the x position of the queen on y;
x1=solution[y1];//store the x1 position of the queen on y1;
solution[y1]=x;//swap the x value of queen on y1;
solution[y]=x1;//swap the x value of queen on y;
h-=-++diagonal1[x+y1];//add the new queen to its diagonal1 (x,y1)
h-=-++diagonal2[queens-1+y1-x];//add the new queen to its diagonal2 (x,y1)
h-=-++diagonal1[x1+y];//add the new queen to its diagonal1 (x1,y)
h-=-++diagonal2[queens-1-x1+y];//add the new queen to its diagonal2 (x1,y)
h-=+diagonal1[x+y]--;//remove the old queen form its diagonal1 (x,y)
h-=+diagonal2[queens-1-x+y]--;//remove the old queen from its diagonal2 (x,y)
h-=+diagonal1[x1+y1]--;//remove the old queen from its diagonal1 (x1,y1)
h-=+diagonal2[queens-1-x1+y1]--;//remove the old queen from its diagonal2 (x1,y1)
}
while(true){
{//start print arrays scope
cout<<"sol:";
for(iterator=0;iterator<queens;iterator++){
cout<<solution[iterator];
cout<<",";
}
cout<<endl;
cout<<"dg1:";
for(iterator=0;iterator<2*queens-1;iterator++){
cout<<diagonal1[iterator];
cout<<",";
}
cout<<endl;
cout<<"dg2:";
for(iterator=0;iterator<2*queens-1;iterator++){
cout<<diagonal2[iterator];
cout<<",";
}
cout<<endl;
}//end print arrays scope
{//start first step warp scope
y=rand()%queens;//select a queen randomly
do{
y1=rand()%queens;//select other queen randomly
}while(y==y1);//y won't be equal to y1
x=solution[y];//store the x position of the queen on y;
x1=solution[y1];//store the x1 position of the queen on y1;
solution[y1]=x;//swap the x value of queen on y1;
solution[y]=x1;//swap the x value of queen on y;
//if(x==x1)return 1;
}//end first step warp scope
/*
{//start it works scope
h-=-++diagonal1[x+y1];//add the new queen to its diagonal1 (x,y1)
h-=-++diagonal2[queens-1+y1-x];//add the new queen to its diagonal2 (x,y1)
h-=-++diagonal1[x1+y];//add the new queen to its diagonal1 (x1,y)
h-=-++diagonal2[queens-1-x1+y];//add the new queen to its diagonal2 (x1,y)
h-=+diagonal1[x+y]--;//remove the old queen form its diagonal1 (x,y)
h-=+diagonal2[queens-1-x+y]--;//remove the old queen from its diagonal2 (x,y)
h-=+diagonal1[x1+y1]--;//remove the old queen from its diagonal1 (x1,y1)
h-=+diagonal2[queens-1-x1+y1]--;//remove the old queen from its diagonal2 (x1,y1)
}//end it works scope
*/
{//start it doesn't work scope
if((x+y1)==(x1+y))return 1;//Same diagonal
else if((queens-1-x1+y)==(queens-1-x+y1))return 1;//Same diagonal
else if((x+y)==(x1+y1))return 1;//Same diagonal
else if((queens-1-x1+y1)==(queens-1-x+y))return 1;//Same diagonal
h-=//start update conflicts
-++diagonal1[x+y1]//add the new queen to its diagonal1 (x,y1)
-++diagonal2[queens-1+y1-x]//add the new queen to its diagonal2 (x,y1)
-++diagonal1[x1+y]//add the new queen to its diagonal1 (x1,y)
-++diagonal2[queens-1-x1+y]//add the new queen to its diagonal2 (x1,y)
+diagonal1[x+y]--//remove the old queen form its diagonal1 (x,y)
+diagonal2[queens-1-x+y]--//remove the old queen from its diagonal2 (x,y)
+diagonal1[x1+y1]--//remove the old queen from its diagonal1 (x1,y1)
+diagonal2[queens-1-x1+y1]--//remove the old queen from its diagonal2 (x1,y1)
;//end update conflicts
}//end it doesn't work scope
/**/
}//end while
return 0;
};
|