-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueensPromising.c
101 lines (88 loc) · 2.07 KB
/
QueensPromising.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
*
* @Name:N-Queens Promising C
* @Author: Max Base
* @Date: 2022/12/10
* @Repository: https://github.com/BaseMax/NQueensPromisingC
*
**/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
typedef struct {
int n;
int *row;
int **answers;
int answerCount;
} queens;
queens *queensCreate(int n)
{
size_t MAX_SIZE = pow(n, n); // Upper bound on number of answers
queens *q = malloc(sizeof(queens));
q->n = n;
q->row = malloc(sizeof(int) * (n + 1));
q->answers = malloc(sizeof(int *) * MAX_SIZE);
for (int i = 0; i < MAX_SIZE; i++) {
q->answers[i] = malloc(sizeof(int) * (n + 1));
}
q->answerCount = 0;
return q;
}
bool queensPromising(queens *q, int i)
{
int k = 0;
while (k < i) {
if (q->row[i] == q->row[k] || abs(q->row[i] - q->row[k]) == abs(i - k)) return false;
k++;
}
return true;
}
void queensCheck(queens *q, int i)
{
int j;
if (queensPromising(q, i)) {
if (i == q->n) {
for (j = 1; j <= q->n; j++) {
q->answers[q->answerCount][j - 1] = q->row[j];
// printf("%d ", q->row[j]);
}
// printf("\n");
q->answerCount++;
} else {
for (j = 1; j <= q->n; j++) {
q->row[i + 1] = j;
queensCheck(q, i + 1);
}
}
}
}
void queensPrint(queens *q)
{
for (int i = 0; i < q->answerCount; i++) {
// printf("Answer %d: ", i + 1);
for (int j = 0; j < q->n; j++) {
printf("%d ", q->answers[i][j]);
}
printf("\n");
}
}
int main(int argc, char** argv)
{
if (argc != 2) {
printf("Usage: queens <number of queens>\n");
return 1;
}
int n = atoi(argv[1]);
printf("N is %d\n", n);
queens *q = queensCreate(n);
queensCheck(q, 0);
printf("Number of answers: %d\n", q->answerCount);
if (q->answerCount > 0) {
printf("List of answers:\n");
queensPrint(q);
}
return 0;
}