-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgfg_backtracking.c
More file actions
58 lines (51 loc) · 1.57 KB
/
gfg_backtracking.c
File metadata and controls
58 lines (51 loc) · 1.57 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
// A backtracking based C Program to fill two instances of all numbers
// from 1 to n in a specific way
#include <stdio.h>
#include <stdbool.h>
// A recursive utility function to fill two instances of numbers from
// 1 to n in res[0..2n-1]. 'curr' is current value of n.
bool fillUtil(int res[], int curr, int n)
{
// If current number becomes 0, then all numbers are filled
if (curr == 0) return true;
// Try placing two instances of 'curr' at all possible locations
// till solution is found
int i;
for (i=0; i<2*n-curr-1; i++)
{
// Two 'curr' should be placed at 'curr+1' distance
if (res[i] == 0 && res[i + curr + 1] == 0)
{
// Plave two instances of 'curr'
res[i] = res[i + curr + 1] = curr;
// Recur to check if the above placement leads to a solution
if (fillUtil(res, curr-1, n))
return true;
// If solution is not possible, then backtrack
res[i] = res[i + curr + 1] = 0;
}
}
return false;
}
// This function prints the result for input number 'n' using fillUtil()
void fill(int n)
{
// Create an array of size 2n and initialize all elements in it as 0
int res[2*n], i;
for (i=0; i<2*n; i++)
res[i] = 0;
// If solution is possible,l then print it.
if (fillUtil(res, n, n))
{
for (i=0; i<2*n; i++)
printf("%d ", res[i]);
}
else
puts("Not Possible");
}
// Driver program
int main()
{
fill(7);
return 0;
}