Skip to content

Instantly share code, notes, and snippets.

@dchiji
Created April 11, 2010 00:56
Show Gist options
  • Save dchiji/362411 to your computer and use it in GitHub Desktop.
Save dchiji/362411 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int count = 0;
int put_queen(int lines[], int size, int n)
{
int i;
int j;
if(n == size) {
int k;
int l;
for(k = 0; k < size; k++) {
for(l = 0; l < lines[k]; l++) printf(" ");
printf("*\n");
}
printf("\n\n\n");
count++;
return 0;
}
for(i = 0; i < 8; i++) {
lines[n] = i;
for(j = 0; j < n; j++) {
if(lines[j] == i || lines[j] - (n - j) == i || lines[j] + (n - j) == i) goto CONTINUE;
}
put_queen(lines, size, n + 1);
CONTINUE:
continue;
}
}
int main()
{
int lines[8];
put_queen(lines, 8, 0);
printf("%d\n", count);
}
#include <stdio.h>
#define N 8
int count = 0;
int put_queen(int lines[], int size, int n)
{
int i;
int j;
if(n == size) {
int k;
int l;
for(k = 0; k < size; k++) {
for(l = 0; l < lines[k]; l++) printf(" ");
printf("*\n");
}
printf("\n\n\n");
count++;
return 0;
}
for(i = 0; i < N; i++) {
lines[n] = i;
for(j = 0; j < n; j++) {
if(lines[j] == i || lines[j] - (n - j) == i || lines[j] + (n - j) == i) goto CONTINUE;
}
put_queen(lines, size, n + 1);
CONTINUE:
continue;
}
}
int main()
{
int lines[N];
put_queen(lines, N, 0);
printf("%d\n", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment