#include <stdio.h>
#include <stdlib.h>
void _trail(int dimention, int n);
void _printChessboard(int arr[8][8], int dimention);
int _isValid(int i, int j, int diamention);
void _clear(int dimention, int row); //清除指定行以下的元素
void _clearRow(int row, int col); //清除指定行的指定列之前的元素
int chessboard[8][8] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
int main(int argc, char *argv[]) {
_trail(8, 0);
getchar();
}
void _trail(int dimention, int n) {
if (n >= dimention)
_printChessboard(chessboard, 8);
_clear(dimention,n);
for (int i = 0; i < dimention; i++) {
_clearRow(n, i);
_clear(dimention, n + 1);
chessboard[n][i] = 1;
if (_isValid(n, i, dimention))
_trail(dimention, n + 1);
}
}
void _printChessboard(int arr[8][8], int dimention) {
for (int i = 0; i < dimention; i++) {
for (int j = 0; j < dimention; j++)
printf("%d ", arr[i][j]);
putchar(10);
}
putchar(10);
return;
}
int _isValid(int i, int j, int dimention) {
int sum_Row = 0;
int sum_Column = 0;
int sum_MDiagonal = 0; //主对角线元素之和
int sum_SDiagonal = 0; //副对角线元素之和
for (int k = 0; k < 8; ++k) {
sum_Row += chessboard[i][k];
sum_Column += chessboard[k][j];
}
if (sum_Row != 1 || sum_Column != 1)
return 0; //判断横纵反向是否满足
for (int m = 0; m < dimention; ++m) {
int mainElem_rowNum = i + (m - j);
int mainElem_columnNum = m;
int sideElem_rowNum = i + (j - m);
int sideElem_columnNum = m;
if (mainElem_rowNum >= 0 && mainElem_rowNum < dimention)
sum_MDiagonal += chessboard[mainElem_rowNum][mainElem_columnNum];
if (sideElem_rowNum >= 0 && sideElem_rowNum < dimention)
sum_SDiagonal += chessboard[sideElem_rowNum][sideElem_columnNum];
}
if (sum_MDiagonal != 1 || sum_SDiagonal != 1)
return 0; //判断对角反向是否满足
return 1;
}
void _clear(int dimention, int row) {
for (int i = row; i < dimention; ++i)
for (int j = 0; j < dimention; ++j)
chessboard[i][j] = 0;
}
void _clearRow(int row, int col) {
for (int i = 0; i < col; ++i)
chessboard[row][i] = 0;
}
#include <stdlib.h>
void _trail(int dimention, int n);
void _printChessboard(int arr[8][8], int dimention);
int _isValid(int i, int j, int diamention);
void _clear(int dimention, int row); //清除指定行以下的元素
void _clearRow(int row, int col); //清除指定行的指定列之前的元素
int chessboard[8][8] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
int main(int argc, char *argv[]) {
_trail(8, 0);
getchar();
}
void _trail(int dimention, int n) {
if (n >= dimention)
_printChessboard(chessboard, 8);
_clear(dimention,n);
for (int i = 0; i < dimention; i++) {
_clearRow(n, i);
_clear(dimention, n + 1);
chessboard[n][i] = 1;
if (_isValid(n, i, dimention))
_trail(dimention, n + 1);
}
}
void _printChessboard(int arr[8][8], int dimention) {
for (int i = 0; i < dimention; i++) {
for (int j = 0; j < dimention; j++)
printf("%d ", arr[i][j]);
putchar(10);
}
putchar(10);
return;
}
int _isValid(int i, int j, int dimention) {
int sum_Row = 0;
int sum_Column = 0;
int sum_MDiagonal = 0; //主对角线元素之和
int sum_SDiagonal = 0; //副对角线元素之和
for (int k = 0; k < 8; ++k) {
sum_Row += chessboard[i][k];
sum_Column += chessboard[k][j];
}
if (sum_Row != 1 || sum_Column != 1)
return 0; //判断横纵反向是否满足
for (int m = 0; m < dimention; ++m) {
int mainElem_rowNum = i + (m - j);
int mainElem_columnNum = m;
int sideElem_rowNum = i + (j - m);
int sideElem_columnNum = m;
if (mainElem_rowNum >= 0 && mainElem_rowNum < dimention)
sum_MDiagonal += chessboard[mainElem_rowNum][mainElem_columnNum];
if (sideElem_rowNum >= 0 && sideElem_rowNum < dimention)
sum_SDiagonal += chessboard[sideElem_rowNum][sideElem_columnNum];
}
if (sum_MDiagonal != 1 || sum_SDiagonal != 1)
return 0; //判断对角反向是否满足
return 1;
}
void _clear(int dimention, int row) {
for (int i = row; i < dimention; ++i)
for (int j = 0; j < dimention; ++j)
chessboard[i][j] = 0;
}
void _clearRow(int row, int col) {
for (int i = 0; i < col; ++i)
chessboard[row][i] = 0;
}