forked from SkienaBooks/Algorithm-Design-Manual-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-queens.c
More file actions
119 lines (88 loc) · 2.89 KB
/
8-queens.c
File metadata and controls
119 lines (88 loc) · 2.89 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
8-queens.c
Solve the eight queens problem using backtracking
begun: March 1, 2002
by: Steven Skiena
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
#define MAXCANDIDATES 100 /* max possible next extensions */
#define NMAX 100 /* maximum solution size */
bool finished = FALSE;
int solution_count; /* how many solutions are there? */
void process_solution(int a[], int k, int input) {
int i; /* counter */
solution_count ++;
}
int is_a_solution(int a[], int k, int n) {
return (k == n);
}
void make_move(int a[], int k, int n) {
}
void unmake_move(int a[], int k, int n) {
}
/* What are possible elements of the next slot in the 8-queens
problem?
*/
void construct_candidates(int a[], int k, int n, int c[], int *ncandidates) {
int i, j; /* counters */
bool legal_move; /* might the move be legal? */
*ncandidates = 0;
for (i = 1; i <= n; i++) {
legal_move = TRUE;
for (j = 1; j < k; j++) {
if (abs((k)-j) == abs(i-a[j])) { /* diagonal threat */
legal_move = FALSE;
}
if (i == a[j]) { /* column threat */
legal_move = FALSE;
}
}
if (legal_move == TRUE) {
c[*ncandidates] = i;
*ncandidates = *ncandidates + 1;
}
}
}
void backtrack(int a[], int k, int input) {
int c[MAXCANDIDATES]; /* candidates for next position */
int ncandidates; /* next position candidate count */
int i; /* counter */
if (is_a_solution(a, k, input)) {
process_solution(a, k, input);
} else {
k = k + 1;
construct_candidates(a,k,input,c,&ncandidates);
for (i = 0; i < ncandidates; i++) {
a[k] = c[i];
make_move(a, k, input);
backtrack(a, k, input);
unmake_move(a, k, input);
if (finished == TRUE) {
return; /* terminate early */
}
}
}
}
int main(void) {
int a[NMAX]; /* solution vector */
int i; /* counter */
for (i = 1; i <= 10; i++) {
solution_count = 0;
backtrack(a, 0, i);
printf("n=%d solution_count=%d\n", i, solution_count);
}
return 0;
}