#include
#include
using namespace std;
bool chess[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
};
void Queen();
int main()
{
Queen();
vector x;
vector y;
for (int m = 0; m < 8; m++)
{
for (int n = 0; n < 8; n++)
{
if (chess[m][n])
{
x.push_back(m);
y.push_back(n);
}
}
}
if (x.size() != 8 || y.size() != 8)
{
cout << false;
return 0;
}
for (int i = 0; i < 7; i++)
{
for (int j = i+1; j < 8; j++)
{
if (x[i] == x[j] || y[i] == y[j] || x[i] - x[j] == y[i] - y[j] || x[i] - x[j] == y[j] - y[i])
{
cout << false;
return 0;
}
}
}
cout << true;
return 0;
}
#include
#include
#include
const int ddd = 0;
int x[8];
bool check(int row,int col){
if(ddd)cout<<" checking ["<= 8)return true;
// 没有到达最后一层
// 本层
for(int i = 0;i<8;i++){
// 尝试每一个位置
if(check(layer,i))x[layer] = i;
else continue;
if(ddd)cout<<"\toccupy["<0){
int sel = rand()%cand;// 在缓冲区内“采样”
//如果找到了可以放置的位置
x[row++] = buf[sel];
}
// 否则算法早停
else return false;
}
// 剩余部分回溯
return dfs(BOUND);
}
void Queen(){
srand((unsigned int)time(NULL));
for(int i = 0;i<8;i++)x[i] = -1;// init
while(!LV());
for(int i = 0;i<8;i++){
// 放置
chess[i][x[i]] = 1;
}
if(ddd)for(int i = 0;i<8;i++){
for(int j = 0;j<8;j++){
cout<< chess[i][j] <<" ";
}
cout<