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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
OLD!!!
http://ainoob.com/files/programs/c_programs/eight_queen.cpp
NEW!!!!
#include "iostream"
using namespace std;
int (*p)[8] = new int [100][8];
int ttime=0;
void show(int a,int b)
{
a--;
b--;
for(;a<(b+1);a++)
{
for(int x=0;x<8;x++)
{
int xx=p[a][x];
for(int xxx=xx;xxx>0;xxx--)
{
cout<<"0 ";
}
cout<<"X ";
for(int xxxx=(7-xx);xxxx>0;xxxx--)
{
cout<<"0 ";
}
cout<<endl;
}
cout<<endl;
}
}
int check(int a,int b,int c,int d, int e,int f,int g,int h)
{
int xxxxx=0;
int test1=0;
int c1[8]={0,0,0,0,0,0,0,0};
int c2[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int c3[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
c1[a]++;
c1[b]++;
c1[c]++;
c1[d]++;
c1[e]++;
c1[f]++;
c1[g]++;
c1[h]++;
for(int loop2=0;loop2<8;loop2++)
{
if(c1[loop2]>1)
{
test1++;
}
}
if (test1==0)
{
int test2=0;
c2[8-a]++;
c2[9-b]++;
c2[10-c]++;
c2[11-d]++;
c2[12-e]++;
c2[13-f]++;
c2[14-g]++;
c2[15-h]++;
for (int loop4=0;loop4<15;loop4++)
{
if(c2[loop4]>1)
{
test2++;
}
}
if(test2==0)
{
int test3=0;
c3[1+a]++;
c3[2+b]++;
c3[3+c]++;
c3[4+d]++;
c3[5+e]++;
c3[6+f]++;
c3[7+g]++;
c3[8+h]++;
for (int loop6=0;loop6<15;loop6++)
{
if(c3[loop6]>1)
{
test3++;
}
}
if(test3==0)
{
p[ttime][0]=a;
p[ttime][1]=b;
p[ttime][2]=c;
p[ttime][3]=d;
p[ttime][4]=e;
p[ttime][5]=f;
p[ttime][6]=g;
p[ttime][7]=h;
ttime++;
cout<<".";
xxxxx=1;
}
}
}
return xxxxx;
}
int main()
{
for (int aa=0;aa<8;aa++)
{
for (int bb=0;bb<8;bb++)
{
for (int cc=0;cc<8;cc++)
{
for (int dd=0;dd<8;dd++)
{
for(int ee=0;ee<8;ee++)
{
for (int ff=0;ff<8;ff++)
{
for (int gg=0;gg<8;gg++)
{
for (int hh=0;hh<8;hh++)
{
check(aa,bb,cc,dd,ee,ff,gg,hh);
}
}
}
}
}
}
}
}
cout<<endl<<"The calculating is finished,there are "<<ttime<<" qualified examples."<<endl<<"Do you want to see them?"<<endl;
cout<<"'1' means yes, and '0' means no"<<endl;
int cinsee;
cin>>cinsee;
looper:
if(cinsee)
{
cout<<"Input the starting example and ending example you want to see"<<endl<<"Start with the starting example"<<endl;
firstcin:
int cinstart;
cin>>cinstart;
if(cinstart<1||cinstart>ttime)
{
cout<<"Out of range, please input another starting number"<<endl;
goto firstcin;
}
cout<<"Now the Ending example"<<endl;
secondcin:
int cinend;
cin>>cinend;
if(cinend<cinstart||cinend>ttime)
{
cout<<"Out of range, please input another starting number"<<endl;
goto secondcin;
}
show(cinstart,cinend);
}
cout<<"To show another example, please input 1. To quit, please input 0"<<endl;
cin>>cinsee;
if(cinsee)
goto looper;
}
|