1
+ #include <stdbool.h>
1
2
#include <stdio.h>
2
3
#include <stdlib.h>
3
4
#include <assert.h>
4
5
5
- static inline int conflict (int * stack , int i , int j )
6
+ static inline int conflict (int * stack , int row , int col )
6
7
{
7
- int k ;
8
- for (k = 0 ; k < i ; k ++ ) {
8
+ int i ;
9
+ for (i = 0 ; i < row ; i ++ ) {
9
10
/* 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 ;
12
13
}
13
14
}
14
-
15
- return 0 ;
15
+ return false;
16
16
}
17
17
18
18
static inline void push (int * stack , int row , int col )
@@ -35,11 +35,10 @@ static inline int top(int *stack, int n)
35
35
return row ;
36
36
}
37
37
}
38
-
39
38
return 0 ;
40
39
}
41
40
42
- static char * * solution (int * stack , int n )
41
+ static char * * solute (int * stack , int n )
43
42
{
44
43
int row , col ;
45
44
char * * solution = malloc (n * sizeof (char * ));
@@ -54,23 +53,50 @@ static char **solution(int *stack, int n)
54
53
return solution ;
55
54
}
56
55
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
+
57
74
/**
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
+ {
62
81
int row = 0 , col = 0 , sum = 0 ;
63
82
char * * * solutions = malloc (1000 * sizeof (char * * ));
64
-
83
+ * returnColumnSizes = malloc ( 1000 * sizeof ( int ));
65
84
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
66
91
for (row = 0 ; row < n ; row ++ ) {
67
92
stack [row ] = -1 ;
68
93
}
69
94
70
95
if (n == 1 ) {
71
96
stack [0 ] = 0 ;
72
- solutions [0 ] = solution (stack , n );
97
+ solutions [0 ] = solute (stack , n );
73
98
* returnSize = 1 ;
99
+ * returnColumnSizes [0 ] = 1 ;
74
100
return solutions ;
75
101
}
76
102
@@ -82,7 +108,6 @@ char*** solveNQueens(int n, int *returnSize) {
82
108
/* No other positions in this row and therefore backtracking */
83
109
if (-- row < 0 ) {
84
110
/* All solution provided */
85
- free (stack );
86
111
* returnSize = sum ;
87
112
return solutions ;
88
113
}
@@ -100,7 +125,9 @@ char*** solveNQueens(int n, int *returnSize) {
100
125
/* Full stack, a new complete solution */
101
126
row = top (stack , n );
102
127
if (row == n - 1 ) {
103
- solutions [sum ++ ] = solution (stack , n );
128
+ solutions [sum ] = solute (stack , n );
129
+ (* returnColumnSizes )[sum ] = n ;
130
+ sum ++ ;
104
131
}
105
132
106
133
/* Move on to find if there are still other solutions */
@@ -109,6 +136,7 @@ char*** solveNQueens(int n, int *returnSize) {
109
136
}
110
137
111
138
assert (0 );
139
+ #endif
112
140
}
113
141
114
142
int main (int argc , char * * argv )
@@ -121,7 +149,8 @@ int main(int argc, char **argv)
121
149
}
122
150
123
151
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 );
125
154
for (i = 0 ; i < num_of_solution ; i ++ ) {
126
155
char * * solution = solutions [i ];
127
156
for (row = 0 ; row < n ; row ++ ) {
0 commit comments