// 0513实验.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#define n 8
static int x[n];//x[i]表示第i个皇后放在第i行第x[i]列上
int Place(int k)
{
for(int i=0; i<k; i++)
if(x[i]==x[k] || abs(i-k)==abs(x[i]-x[k]))
return 1;
return 0;
}
void Queen()
{
int k=0;
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
while (k>=0)
{
x[k]++;
while(x[k]<n && Place(k)==1)
x[k]++;
if(x[k]<n && k==n-1)
{
for(int i=0; i<n; i++)
{
cout<<x[i]+1<<" ";
a[i][x[i]]=1;
}
cout<<endl;
cout<<"八皇后在棋盘上的排列:"<<endl;
for( i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return;
}
if(x[k]<n && k<n-1)
k=k+1;
else
x[k--]=-1;
}
cout<<"无解!"<<endl;
}
int main()
{
for(int i=0; i<n; i++)
{
x[i]=-1;
}
Queen();
return 0;
}
八皇后问题 根据书上的伪代码整理了一下 但是好像只能求解其中一种情况,应该是有很多种情况的吧 这个之后看看能不能再继续做下 找出所有的解