#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void bubble_sort(int array1[8], int str[128][8], int a, int array[8][8]);
void queen_sort(int array[8][8], int str[128][8], int row, int col);
void storage(int array1[8], int str[128][8], int array[8][8]);
//用来存储找到的皇后排序
void storage(int array1[8], int str[128][8], int array[8][8])
{
int i = 0, j = 0, k = 0;
extern int number;
for ( i = 0 ; i <= number ; i++ )
{
for ( j = 0 ; j < 8 ; j++ )
{
if ( str[i][j] == array1[j])
{
k++ ;
if ( k == 8)
{
queen_sort(array, str, 8, 8);
}
}
else
{
break;
}
}
}
for ( i = 0 ; i < 8 ; i++ )
{
str[number][i] = array1[i];
}
queen_sort(array, str, 8, 8);
}
//排序
void bubble_sort(int array1[8], int str[128][8], int a, int array[8][8])
{
int aver, i, j = 0, k = a - 1, sum = 0;
for (i = 0 ; i < a ; i++ )
{
sum += array1[i];
}
aver = sum / a ;
int str1[a];
for ( i = 0 ; i < a ; i++ )
{
if(array1[i] <= aver)
{
str1[j++] = array1[i];
}
else
{
str1[k--] = array1[i];
}
}
sum = a / 2 ;
while (sum--)
{
for ( i = 0 ; i < a - 1 ; i++ )
{
if(str1[i] <= str1[i + 1])
{
;
}
else
{
aver = str1[i];
str1[i] = str1[i + 1];
str1[i + 1] = aver ;
}
}
}
for ( i = 0 ; i < a ; i++ )
{
array1[i] = str1[i];
}
storage(array1, str, array);
}
//找可能的排列方式
void queen_sort(int array[8][8], int str[128][8], int row, int col)
{
int i, j, k = 1, num = 0 ;
int array1[8] = {0};
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
{
array[i][j] = k++ ;
}
}
srand((unsigned)time(NULL)) ;
while (array1[7] == 0)
{
i = rand() % 8;
j = rand() % 8;
if (array[i][j] != 0)
{
array1[num++] = array[i][j];
for (k = 0 ; k < col ; k++ )
{
array[i][k] = 0;
array[k][j] = 0;
array[++i][++j] = 0;
array[--i][--j] = 0;
array[--i][++j] = 0;
array[++i][--j] = 0;
}
}
}
bubble_sort(array1, str, 8, array);
}
int main(void)
{
int array[8][8];
int i, j;
extern int number ;
int str[128][8] = {0};
queen_sort(array, str, 8, 8);
for ( i = 0 ; i <= number ; i++ )
{
for ( j = 0 ; j < 8 ; j++ )
{
printf("%d ", str[i][j]);
}
printf("\n");
}
return 0;
}