Skip to content

Instantly share code, notes, and snippets.

@m0r13
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save m0r13/034247d347dd38f20049 to your computer and use it in GitHub Desktop.

Select an option

Save m0r13/034247d347dd38f20049 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int N;
int damen[100];
int columns[200], diag1[200], diag2[200];
#define DIAG1(row, column) (row)+(column)
#define DIAG2(row, column) (row)-(column)+N
int ndame(int row) {
if (row == N)
return 1;
for (int i = 0; i < N; i++) {
if (columns[i] || diag1[DIAG1(row, i)] || diag2[DIAG2(row, i)])
continue;
damen[row] = i;
columns[i] = 1;
diag1[DIAG1(row, i)] = 1;
diag2[DIAG2(row, i)] = 1;
if (ndame(row+1))
return 1;
damen[row] = -1;
columns[i] = 0;
diag1[DIAG1(row, i)] = 0;
diag2[DIAG2(row, i)] = 0;
}
return 0;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s N\n", argv[0]);
return 1;
}
N = atoi(argv[1]);
for(int c = 0; c < 200; c++)
columns[c] = diag1[c] = diag2[c] = 0;
ndame(0);
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++)
printf("%c", damen[row] == col ? '#' : '-');
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment