Skip to content

Commit c6fb31e

Browse files
authored
Merge pull request hacktoberfest17#470 from felipecustodio/felipecustodio_nqueens_c
N-Queens problem in C
2 parents 326095b + 4b31234 commit c6fb31e

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

nqueens/nqueens.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
5+
#define bool char
6+
#define TRUE 1
7+
#define FALSE 0
8+
9+
typedef struct board {
10+
int n;
11+
int** matrix;
12+
} BOARD;
13+
14+
void printStep(BOARD* b, int x, int y) {
15+
int i, j;
16+
for (i = 0; i < b->n; i++) {
17+
for (j = 0; j < b->n; j++) {
18+
if (i == x && j == y) {
19+
printf(" ");
20+
} else {
21+
if (b->matrix[i][j]) {
22+
printf(" ");
23+
} else {
24+
printf(" ");
25+
}
26+
}
27+
}
28+
printf("\n");
29+
}
30+
printf("\n\n");
31+
}
32+
33+
bool isValid(BOARD* b, int x, int y) {
34+
35+
int i, j;
36+
// check horizontal
37+
for (i = 0; i < b->n; i++) {
38+
if (i != y && b->matrix[x][i] == 1) return FALSE;
39+
}
40+
41+
// check vertical
42+
for (i = 0; i < b->n; i++) {
43+
if (i != x && b->matrix[i][y] == 1) return FALSE;
44+
}
45+
// check diagonals
46+
// check top left
47+
i = x;
48+
j = y;
49+
while (i >= 0 && j >= 0) {
50+
if (i != x && j != y && b->matrix[i][j] == 1) {
51+
return FALSE;
52+
}
53+
i--;
54+
j--;
55+
}
56+
// check top right
57+
i = x;
58+
j = y;
59+
while (i >= 0 && j < b->n) {
60+
if (i != x && j != y && b->matrix[i][j] == 1) {
61+
return FALSE;
62+
}
63+
i--;
64+
j++;
65+
}
66+
// check bottom left
67+
i = x;
68+
j = y;
69+
while (i < b->n && j >= 0) {
70+
if (i != x && j != y && b->matrix[i][j] == 1) {
71+
return FALSE;
72+
}
73+
i++;
74+
j--;
75+
}
76+
// check bottom right
77+
i = x;
78+
j = y;
79+
while (i < b->n && j < b->n) {
80+
if (i != x && j != y && b->matrix[i][j] == 1) {
81+
return FALSE;
82+
}
83+
i++;
84+
j++;
85+
}
86+
return TRUE;
87+
}
88+
89+
bool placeQueen(BOARD** b, int line, long int* calls) {
90+
int i;
91+
if (line >= (*b)->n) return TRUE;
92+
for (i = 0; i < (*b)->n; i++) {
93+
(*b)->matrix[line][i] = 1;
94+
(*calls)++;
95+
if (isValid((*b), line, i) && placeQueen(b, line+1, calls)) {
96+
return TRUE;
97+
}
98+
(*b)->matrix[line][i] = 0;
99+
}
100+
return FALSE;
101+
}
102+
103+
void printBoard(BOARD* b) {
104+
int i, j;
105+
for (i = 0; i < b->n; i++) {
106+
for (j = 0; j < b->n; j++) {
107+
if (b->matrix[i][j]) {
108+
printf(" ");
109+
} else {
110+
printf(" ");
111+
}
112+
}
113+
printf("\n");
114+
}
115+
}
116+
117+
void queens(int n) {
118+
// benchmarking
119+
clock_t start_t, end_t;
120+
float delta_t = 0.0;
121+
long int calls = 0;
122+
int i, j;
123+
BOARD* b = (BOARD*)malloc(sizeof(BOARD));
124+
b->n = n;
125+
b->matrix = (int**)malloc(sizeof(int*) * n);
126+
for (i = 0; i < n; i++) {
127+
b->matrix[i] = (int*)malloc(sizeof(int) * n);
128+
for (j = 0; j < n; j++) {
129+
b->matrix[i][j] = 0;
130+
}
131+
}
132+
133+
start_t = clock();
134+
if (placeQueen(&b, 0, &calls)) {
135+
end_t = clock();
136+
// human readable time
137+
delta_t = ((float)(end_t - start_t) / 1000000000000.0F ) * CLOCKS_PER_SEC;
138+
printBoard(b);
139+
printf("%d,%ld,%lfs\n", n, calls, delta_t);
140+
}
141+
142+
for (i = 0; i < n; i++) {
143+
free(b->matrix[i]);
144+
b->matrix[i] = NULL;
145+
}
146+
free(b->matrix);
147+
b->matrix = NULL;
148+
free(b);
149+
}
150+
151+
int main(int argc, char const *argv[]) {
152+
system("clear");
153+
int i;
154+
int n = 0;
155+
scanf("%d", &n);
156+
printf("board size,calls,time\n");
157+
for (i = 0; i <= n; i++) {
158+
queens(i);
159+
}
160+
161+
return 0;
162+
}

0 commit comments

Comments
 (0)