-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbacktracking.c
More file actions
54 lines (42 loc) · 820 Bytes
/
backtracking.c
File metadata and controls
54 lines (42 loc) · 820 Bytes
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
#include <stdio.h>
int sol[100];
void display(int k){
for(int i=0; i<=k; i++){
printf("%d ", sol[i]);
}
printf("\n");
}
int solution(int k, int n){
if(k == n-1){
return 1;
}
return 0;
}
int valid(int k){
for(int i=0; i<k;i++){
if(sol[i] == sol[k]){
return 0;
}
}
return 1;
}
void bt(int k, int n){
for(int i=1; i<=n; i++){ //change the range
sol[k] = i;
if(valid(k)){ //change the valid() function
if(solution(k, n)){ //change the solution() function
display(k); //change the display() function
}
else{
bt(k+1, n);
}
}
}
}
int main(){
int n;
printf("n= ");
scanf("%d", &n);
bt(0, n);
return 0;
}