초보티가 묻어나는 소스코드;
모든 경우의 수에 대해 공격여부 조사 --;
~cpp
char board[8][8] = {
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0}
};
char queens[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
void increase()
{
register int i, j;
i = 7;
for (i = 7; i >= 0; i--) {
j = queens[i];
board[i][j] = 0;
j = queens[i] = (j + 1) % 8;
board[i][j] = 1;
if (j)
break;
}
}
int check_line()
{
register int i, j;
register int count;
for (i = 0; i < 8; i++) {
for (count = 0, j = 0; j < 8; j++) {
if (board[j][i]) {
if (count)
return 1;
count = 1;
}
}
if (! count)
return 1;
}
return 0;
}
int check_diagonal()
{
register int count;
register int i, j, k;
for (i = 0, j = 0; j <= 6; j++) {
for (count = 0, k = 0; k < 8-j; k++) {
if (board[i+k][j+k]) {
if (count)
return 1;
count = 1;
}
}
}
for (i = 1, j = 0; i <= 6; i++) {
for (count = 0, k = 0; k < 8-i; k++) {
if (board[i+k][j+k]) {
if (count)
return 1;
count = 1;
}
}
}
for (i = 0, j = 1; j <= 7; j++) {
for (count = 0, k = 0; k < j+1; k++) {
if (board[i+k][j-k]) {
if (count)
return 1;
count = 1;
}
}
}
for (i = 1, j = 7; i <= 6; i++) {
for (count = 0, k = 0; k < 8-i; k++) {
if (board[i+k][j-k]) {
if (count)
return 1;
count = 1;
}
}
}
return 0;
}
int main()
{
int i, j;
for (;; increase()) {
if (check_line())
continue;
else if (check_diagonal())
continue;
break;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
return 0;
}