0% found this document useful (0 votes)
3 views6 pages

pgm12 & 8 JITD

The document contains two C/C++ programs: one for solving the N Queen's problem using backtracking and another for finding a subset of a given set of positive integers that sums to a specified value. The N Queen's program prints all possible arrangements of queens on a chessboard, while the subset program checks for combinations of integers that equal a given sum. Both programs utilize recursive functions and display results to the user.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

pgm12 & 8 JITD

The document contains two C/C++ programs: one for solving the N Queen's problem using backtracking and another for finding a subset of a given set of positive integers that sums to a specified value. The N Queen's program prints all possible arrangements of queens on a chessboard, while the subset program checks for combinations of integers that equal a given sum. Both programs utilize recursive functions and display results to the user.

Uploaded by

deadshotlev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

12.

Design and implement C/C++ Program for N Queen's problem


using Backtracking.

#include<stdio.h>

#include<conio.h>

#include<math.h>

int a[30],count=0;

int place(int pos)

int i;

for(i=1;i<pos;i++)

if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))

return 0;

return 1;

void print_sol(int n)

int i,j;
count++;

printf("\n\nSolution #%d:\n",count);

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(a[i]==j)

printf("Q\t");

else

printf("*\t");

printf("\n");

void queen(int n)

int k=1;

a[k]=0;

while(k!=0)

a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))

a[k]++;

if(a[k]<=n)

if(k==n)

print_sol(n);

else

k++;

a[k]=0;

else

k--;

void main()

int i,n;

clrscr();

printf("Enter the number of Queens\n");


scanf("%d",&n);

queen(n);

printf("\nTotal solutions=%d",count);

getch();

}
Output
Enter the number of queen 4
. . Q .
Q . . .
. . . Q
. Q . .

8. C program to Find a subset of a given set S = (s1, s2, ….sn} of n


positive integers whose sum is equal to a given positive integer d.

#include<stdio.h>
#include<conio.h>
int s[10],d,n,set[10],count=0;
void display(int);
int flag = 0;
void main()
{
int subset(int,int);
int i;
clrscr();
printf("ENTER THE NUMBER OF THE ELEMENTS IN THE SET : ");
scanf("%d",&n);
printf("ENTER THE SET OF VALUES : ");
for(i=0;i<n;i++)
scanf("%d",&s[i]);
printf("ENTER THE SUM : ");
scanf("%d",&d);
printf("THE PROGRAM OUTPUT IS: ");
subset(0,0);
if(flag == 0)
printf("There is no solution");
getch();
}
int subset(int sum,int i)
{
if(sum == d)
{
flag = 1;
display(count);
return;
}
if(sum>d || i>=n)return;
else
{
set[count]=s[i];
count++;
subset(sum+s[i],i+1);
count--;
subset(sum,i+1);
}
}
void display(int count)
{
int i;
printf("\t{");
for(i=0;i<count;i++)
printf("%d,",set[i]);
printf("}");
}

Output:

You might also like