No older revisions available
No older revisions available
~cpp
#include <iostream.h>
#include <math.h>
bool EightQueens(int level, int queens[]);
bool Promissing(int level, int queens[]);
bool WellPutted(int level1, int level2, int queens[]);
void PrintResult(int queens[]);
int main(int argc, char* argv[])
{
int queens[8],i;
for (i=0;i<8;i++) queens[i]=0;
EightQueens(0,queens);
return 0;
}
bool EightQueens(int level, int queens[])
{
int i;
for (i=0;i<8;i++)
{
queens[level]=i;
if (Promissing(level,queens))
{
if (level==7)
{
PrintResult(queens);//return true;
}
else
{
EightQueens(level+1,queens);
//return true;
}
}
}
return false;
}
bool Promissing(int level, int queens[])
{
int i;//,j;
if (level==0)
return true;
for (i=0;i<level;i++)
{
//for (j=i+1;j<level+1;j++)
//{
if (!WellPutted(i,level,queens))
return false;
//}
}
return true;
}
bool WellPutted(int level1, int level2, int queens[])
{
if (queens[level1]==queens[level2])
return false;
if (abs(queens[level1]-queens[level2]) == level2 - level1)
return false;
return true;
}
void PrintResult(int queens[])
{
cout<<"the result is ";
for (int i=0;i<8;i++)
cout<<queens[i]<<" ";
cout<<endl;
}