Skip to content

Commit 4f58f7f

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 754133f commit 4f58f7f

File tree

4 files changed

+80
-26
lines changed

4 files changed

+80
-26
lines changed

051_n_queens/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
all:
2-
gcc -O2 -o queen n_queens.c
2+
gcc -O2 -o test n_queens.c

051_n_queens/n_queens.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
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

1818
static 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

114142
int 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++) {

052_n_queens_ii/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
all:
2-
gcc -O2 -o queen n_queens.c
2+
gcc -O2 -o test n_queens.c

052_n_queens_ii/n_queens.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
#include <stdbool.h>
12
#include <stdio.h>
23
#include <stdlib.h>
34
#include <assert.h>
4-
static inline int conflict(int *stack, int i, int j)
5+
6+
7+
static inline int conflict(int *stack, int row, int col)
58
{
6-
int k;
7-
for (k = 0; k < i; k++) {
9+
int i;
10+
for (i = 0; i < row; i++) {
811
/* If occupied or in one line */
9-
if (j == stack[k] || abs(i - k) == abs(j - stack[k])) {
10-
return 1;
12+
if (col == stack[i] || abs(row - i) == abs(col - stack[i])) {
13+
return true;
1114
}
1215
}
13-
return 0;
16+
return false;
1417
}
1518

1619
static inline void push(int *stack, int row, int col)
@@ -36,7 +39,28 @@ static inline int top(int *stack, int n)
3639
return 0;
3740
}
3841

42+
static void dfs(int n, int row, int *stack, int *count)
43+
{
44+
int col;
45+
if (row == n) {
46+
(*count)++;
47+
} else {
48+
for (col = 0; col < n; col++) {
49+
if (row == 0 || !conflict(stack, row, col)) {
50+
stack[row] = col;
51+
dfs(n, row + 1, stack, count);
52+
}
53+
}
54+
}
55+
}
56+
3957
int totalNQueens(int n) {
58+
#if 1
59+
int count = 0;
60+
int *stack = malloc(n * sizeof(int));
61+
dfs(n, 0, stack, &count);
62+
return count;
63+
#else
4064
int row = 0, col = 0, sum = 0, cap = 1;
4165
int *stack = malloc(n * sizeof(int));
4266
for (row = 0; row < n; row++) {
@@ -79,6 +103,7 @@ int totalNQueens(int n) {
79103
col = pop(stack, row);
80104
col++;
81105
}
106+
#endif
82107
}
83108

84109
int main(int argc, char **argv)

0 commit comments

Comments
 (0)