#include <stdio.h>
static char Quence[8][8];
static int Columms[8];
static int main_diagonal[15];
static int second_diagonal[15];
static int iQuence_num = 0;
void solve_quence(int i, int num); //i表示行
void Print();
int main()
{
int row = 0, column = 0;
//初始化棋盘
for(row = 0; row < 8; ++row)
{
Columms[row] = 0;
for(column = 0; column < 8; ++column)
{
Quence[row][column] = '*';
}
}
//主从对角线初始化
for(row = 0; row < 15; ++row)
{
main_diagonal[row] = 0;
second_diagonal[row] = 0;
}
solve_quence(0, 8);
return 0;
}
void solve_quence(int i, int num) //i表示行
{
int icol = 0;
for(icol = 0; icol < num; ++icol)
{
if(Columms[icol] == 0 && main_diagonal[i - icol + 7] == 0 && second_diagonal[i + icol] == 0 )
{
Quence[i][icol] = '@';
Columms[icol] = 1;
main_diagonal[i - icol + 7] = 1;
second_diagonal[i + icol] = 1;
if(i < num - 1)
{
solve_quence(i + 1, num);
}
else //输出
{
Print();
}
//如果前一次皇后的放置导致后面的放置无论如何都不能满足, 则回溯 ,重置
Quence[i][icol] = '*';
Columms[icol] = 0;
main_diagonal[i - icol + (num -1)] = 0;
second_diagonal[i + icol] = 0;
}
}
}
void Print()
{
int ir = 0, jc = 0;
FILE *fp = NULL;
fp = fopen("D://My Program Leaning//8Quence_problem.text", "a+");
// printf("The %d th solution is :/n", ++iQuence_num);
fprintf(fp, "The %d th solution is :/n", ++iQuence_num);
for(ir = 0; ir < 8; ++ir)
{
for(jc = 0; jc < 8; ++jc)
{
//printf("%c", Quence[ir][jc]);
fprintf(fp,"%c", Quence[ir][jc]);
}
//puts("");
fprintf(fp, "/n");
}
// printf("/n/n");
fprintf(fp, "/n/n");
fclose(fp);
}