#include <iostream>
using namespace std;
int line[9] = {1};
int Left[16] = {1};
int Right[16] = {1};
int board[9][9] = {0};
int num = 0;
void print()
{
cout << "No. " << num << endl;
for(int i = 1; i <= 8; i++)
{
for(int j = 1; j <= 8; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
void Try(int col)
{
int row = 1;
while(row <= 8)
{
if(line[row] && Left[row + col - 1] && Right[8 + row - col])
{
board[row][col] = 1;
line[row] = 0;
Left[row + col - 1] = 0;
Right[8 + row - col] = 0;
if(col < 8)
{
Try(col + 1);
}
else
{
num++;
print();
}
board[row][col] = 0;
line[row] = 1;
Left[row + col - 1] = 1;
Right[8 + row - col] = 1;
}
row++;
}
}
int main()
{
for (int i = 1;i <= 8; i++)
line[i] = 1;
for (int i = 1;i <= 15; i++)
{
Left[i] = 1; Right[i] = 1;
}
for (int i = 1 ;i <= 8; i++)
for(int j = 1;j <= 8;j++)
board[i][j] = 0;
Try(1);
return 0;
}
八皇后问题
最新推荐文章于 2012-04-22 17:07:53 发布