                                                                        
/* N-queens problem */                                                  
                                                                        
#define         NMAX    26                                              
                                                                        
int     p[NMAX], xi[NMAX];                                              
                                                                        
main(argc, argv)                                                        
int     argc;                                                           
char  **argv;                                                           
{                                                                       
int     atoi();                                                         
int     i, n = 8, seq = 0;                                              
                                                                        
/*  n  is the size */                                                   
                                                                        
if (argc > 1) n = atoi(*++argv);        /* first arguement as integer */
for (i=0; i<n; i++) p[i]=i;             /* first permutation */         
while (1) {                                                             
    if (test_q(n, p)) {                                                 
        seq++;                                                          
        printf("[%6d]  ", seq);                                         
        for (i=0;i<n;i++) printf ("%2d " , p[i]);                       
        printf("\n");                                                   
        }                                                               
    if (p_next(n, p)) break;                                            
    }                                                                   
return 0;                                                               
}                                                                       
                                                                        
int     test_q(int n, int *a)                                           
{                                                                       
int     i, j, da;                                                       
for (i=0; i<n-1; i++) for(j=i+1; j<n; j++) {                            
    da = a[j]-a[i];                                                     
    if (((da>0) ? da : -da ) == j-i) return 0;                          
    }                                                                   
return  1;                                                              
}                                                                       
                                                                        
/* next permutation in lexical order 12345..n ->n n-1 ... 321 */        
                                                                        
int     p_next(int n, int *a)                                           
{                                                                       
int     i, idx, j, k, min, x;                                           
for (i = n-2; i >= 0 && a[i] > a[i+1]; i--);                            
if (i<0) return 1;                                                      
x = a[i];       /* element to change */                                 
for (j= i+1, min = n; j <= n-1; j++) {                                  
    if (a[j] > x &&  a[j]< min) {                                       
        k=j;                                                            
        min = a[k];                                                     
        }                                                               
    }                                                                   
a[i] = a[k];    /* next possible successor */                           
a[k] = x;                                                               
/* reverse (i.e. sort) elements a[i+1] ... a[n-1] */                    
for(j = i+1, k = n-1; j < k;  j++, k--) {                               
    x = a[j];                                                           
    a[j] = a[k];                                                        
    a[k] = x;                                                           
    }                                                                   
return 0;                                                               
}                                                                       
                                                                        
