1+ #include <stdbool.h>
12#include <stdio.h>
23#include <stdlib.h>
34#include <assert.h>
45
5- static inline int conflict (int * stack , int i , int j )
6+ static inline int conflict (int * stack , int row , int col )
67{
7- int k ;
8- for (k = 0 ; k < i ; k ++ ) {
8+ int i ;
9+ for (i = 0 ; i < row ; i ++ ) {
910 /* If occupied or in one line */
10- if (j == stack [k ] || abs (i - k ) == abs (j - stack [k ])) {
11- return 1 ;
11+ if (col == stack [i ] || abs (row - i ) == abs (col - stack [i ])) {
12+ return true ;
1213 }
1314 }
14-
15- return 0 ;
15+ return false;
1616}
1717
1818static inline void push (int * stack , int row , int col )
@@ -35,11 +35,10 @@ static inline int top(int *stack, int n)
3535 return row ;
3636 }
3737 }
38-
3938 return 0 ;
4039}
4140
42- static char * * solution (int * stack , int n )
41+ static char * * solute (int * stack , int n )
4342{
4443 int row , col ;
4544 char * * solution = malloc (n * sizeof (char * ));
@@ -54,23 +53,50 @@ static char **solution(int *stack, int n)
5453 return solution ;
5554}
5655
56+ static void dfs (int n , int row , int * stack , char * * * solutions , int * count , int * col_sizes )
57+ {
58+ int col ;
59+ if (row == n ) {
60+ solutions [* count ] = solute (stack , n );
61+ col_sizes [* count ] = n ;
62+ (* count )++ ;
63+ } else {
64+ for (col = 0 ; col < n ; col ++ ) {
65+ if (row == 0 || !conflict (stack , row , col )) {
66+ stack [row ] = col ;
67+ dfs (n , row + 1 , stack , solutions , count , col_sizes );
68+ continue ;
69+ }
70+ }
71+ }
72+ }
73+
5774/**
58- ** Return an array of arrays of size *returnSize.
59- ** Note: The returned array must be malloced, assume caller calls free().
60- **/
61- char * * * solveNQueens (int n , int * returnSize ) {
75+ * Return an array of arrays of size *returnSize.
76+ * The sizes of the arrays are returned as *returnColumnSizes array.
77+ * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
78+ */
79+ char * * * solveNQueens (int n , int * returnSize , int * * returnColumnSizes )
80+ {
6281 int row = 0 , col = 0 , sum = 0 ;
6382 char * * * solutions = malloc (1000 * sizeof (char * * ));
64-
83+ * returnColumnSizes = malloc ( 1000 * sizeof ( int ));
6584 int * stack = malloc (n * sizeof (int ));
85+
86+ #if 1
87+ * returnSize = 0 ;
88+ dfs (n , 0 , stack , solutions , returnSize , * returnColumnSizes );
89+ return solutions ;
90+ #else
6691 for (row = 0 ; row < n ; row ++ ) {
6792 stack [row ] = -1 ;
6893 }
6994
7095 if (n == 1 ) {
7196 stack [0 ] = 0 ;
72- solutions [0 ] = solution (stack , n );
97+ solutions [0 ] = solute (stack , n );
7398 * returnSize = 1 ;
99+ * returnColumnSizes [0 ] = 1 ;
74100 return solutions ;
75101 }
76102
@@ -82,7 +108,6 @@ char*** solveNQueens(int n, int *returnSize) {
82108 /* No other positions in this row and therefore backtracking */
83109 if (-- row < 0 ) {
84110 /* All solution provided */
85- free (stack );
86111 * returnSize = sum ;
87112 return solutions ;
88113 }
@@ -100,7 +125,9 @@ char*** solveNQueens(int n, int *returnSize) {
100125 /* Full stack, a new complete solution */
101126 row = top (stack , n );
102127 if (row == n - 1 ) {
103- solutions [sum ++ ] = solution (stack , n );
128+ solutions [sum ] = solute (stack , n );
129+ (* returnColumnSizes )[sum ] = n ;
130+ sum ++ ;
104131 }
105132
106133 /* Move on to find if there are still other solutions */
@@ -109,6 +136,7 @@ char*** solveNQueens(int n, int *returnSize) {
109136 }
110137
111138 assert (0 );
139+ #endif
112140}
113141
114142int main (int argc , char * * argv )
@@ -121,7 +149,8 @@ int main(int argc, char **argv)
121149 }
122150
123151 n = atoi (argv [1 ]);
124- char * * * solutions = solveNQueens (n , & num_of_solution );
152+ int * col_sizes ;
153+ char * * * solutions = solveNQueens (n , & num_of_solution , & col_sizes );
125154 for (i = 0 ; i < num_of_solution ; i ++ ) {
126155 char * * solution = solutions [i ];
127156 for (row = 0 ; row < n ; row ++ ) {
0 commit comments