#include <stdio.h>
#include <math.h>
#include<stdlib.h>
#include <memory.h>
#define ROW 8
#define COLUMN ROW
int success = 0; /*successful times*/
void show(int* p)
{
int i = 0;
printf("%2d>>> ", success);
for (i=0;i<COLUMN ; i++ )
{
printf("%d, ", p[i]);
}
printf("/n");
}
int main(int argc, char *argv[])
{
int pieces[COLUMN];
int row = 0; /*row in the first column*/
int column = 0; /*the current column*/
int flag = 0; /*is matched in current column*/
int i = 0; /*row int current column*/
int j = 0; /*column from 0 to the one before the current column*/
int count = 0; /*steps need*/
/*set the pieces all as -1, [0,COLUMN) represents row in the n-th column*/
memset(pieces, -1, sizeof(pieces));
//init (0,0) as 0, and the current column add to the next column
pieces[column] = 0;
column++;
while (row < ROW)
{
while (column < COLUMN)
{
count++;
flag = 0;
i = pieces[column] + 1;
// if (i>=ROW && column==1)
// break;
// else if (column==0 && i>5)
// break;
for (i=i; i<ROW ;i++ )
{
for (j=0 ;j<column ;j++ )
{
if (i==pieces[j] || abs(j-column)== abs(pieces[j]-i))
{
flag = 0;
break;
}
flag = 1;
}
if (flag)
break;
}
if (flag)
{
pieces[column] = i;
column++;
}
else
{
pieces[column] = -1;
column--;
if (column == 0) /*nowhere to go back*/
break;
}
}/*internal while end*/
if (flag)
{
success++;
show(pieces);
flag = 0;
pieces[column-1] = -1;
column -= 2;
}
else
{
memset(pieces, -1, sizeof(pieces));
column = 0;
row++;
pieces[column] = row;
column++;
}
}/*external while end*/
printf("总共步数:%d/nPress Any Key to Continue.../n", count);
// fflush(stdin);
system("pause");;
return 0;
}