#include #include #include #include typedef int *echiquier; static inline bool ok(int n, int ligne, int colonne, echiquier e); void nreines(int n, int ligne, echiquier e, int *cpt) { for(int col=0; col < n; col++) if (ok(n,ligne,col,e)) { if (ligne == n -1) (*cpt)++; else { e[ligne]=col; nreines(n,ligne+1,e,cpt); } } } static inline bool ok(int n, int ligne, int colonne, echiquier e) { int l, c, cplus, cmoins; for(l=ligne-1, cmoins = colonne-1, cplus=colonne+1; l >= 0 ; l--, cmoins--, cplus++) if (e[l]==colonne || e[l] == cplus || e[l] == cmoins) return false; return true; } void usage(char *s) { fprintf(stderr,"%s entier",s); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { echiquier e; int cpt=0; char *endptr; if (argc<2) usage(argv[0]); int n = strtoul(argv[1],&endptr,10); if (*endptr != 0) usage(argv[1]); e = malloc(n*sizeof(*e)); nreines(n,0,e, &cpt); printf("%d\n",cpt); return EXIT_SUCCESS; }