Professional Documents
Culture Documents
EIGHT QUEENS
PROBLEM
CHAPTER 1
KL University
2
EIGHT QUEENS PROBLEM
The above fgure gives about the detais and information about the 12 fundamental
solutions.
KL University
3
EIGHT QUEENS PROBLEM
CHAPTER 2
KL University
4
EIGHT QUEENS PROBLEM
CHAPTER 3
3.1 Codes:
Module 1:
#include <stdio.h>
#define N 10
typedef enum {FALSE, TRUE} bool ;
void printMatrix(int a[][N], int n) {
/*
* print one solution.
*/
int i, j;
for(i=0; i<n; ++i) {
for(j=0; j<n; ++j)
printf("%d ", a[i][j]);
printf("\n");
}
//getchar();
printf("\n");
}
int getMarkedCol(int a[][N], int n, int row) {
/*
* returns the column marked in row
*/
int j;
for(j=0; j<n; ++j)
if(a[row][j] == TRUE)
return j;
printf("ERR0R: No col marked in the row %d.\n", row);
return -1;
}
boo1 feasible(int a[] [N]], int n, int row, int col) {
/*
* checks whether next queen can be kept at a[row][col].
*/
int i;
int markedCol ;
KL University
5
EIGHT QUEENS PROBLEM
KL University
6
EIGHT QUEENS PROBLEM
Module 2:
Backtraking
of Knights:
#define N 4
#include<stdio.h>
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}
7
EIGHT QUEENS PROBLEM
{
/* base case: If all queens are placed
then return true */
if (col >= N)
return true;
for (int i = 0; i < N; i++)
{
if ( isSafe(board, i, col) )
{
board[i][col] = 1;
if ( solveNQUtil(board, col + 1) )
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
if ( solveNQUtil(board, 0) == false )
{
printf("Solution does not exist");
return false;
}
KL University
8
EIGHT QUEENS PROBLEM
printSolution(board);
return true;
}
Output:
The 1 values indicate placements of queens
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
Insertion sort
#include<stdio.h>
void main( )
{
int a[10],i,j,k,n;
printf("How many elements you want to sort?\n");
scanf("%d",&n);
printf("\nEnter the Elements into an array:\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
k=a[i];
for(j= i-1; j>=0 && k<a[j]; j--)
a[j+1]=a[j];
a[j+1]=k;
}
printf("\n\n Elements after sorting: \n");
for(i=0;i<n;i++)
printf("%d\n", a[i]);
}
OUTPUT:
How many elements you want to sort ? : 6
Enter elements for an array : 78 23 45 8 32 36
After Sorting the elements are : 8 23 32 36 45 78
Program2:
Merge sort:
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
KL University
9
EIGHT QUEENS
PROBLEM
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
return 0;
}
//merging of two
sorted sub-arrays
}
}
int i,j,k;
KL University
10
EIGHT QUEENS PROBLEM
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else temp[k++]=a[j+
+];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
Output:
Enter number of elements = 4
Enter array elements: 2 44 78 55
Sorted array is: 2 44 55 78.
KL University
11
EIGHT QUEENS PROBLEM
Module 3:
Heap sort:
#include <stdlib.h>
#include <stdio.h>
#define LeftChild( i ) ( 2 * ( i ) + 1 )
typedef int ElementType;
void Swap( ElementType *Lhs, ElementType *Rhs )
{
ElementType Tmp = *Lhs;
*Lhs = *Rhs;
*Rhs = Tmp;
}
void PercDown( ElementType A[ ], int i, int N )
{
int Child;
ElementType Tmp;
for( Tmp = A[ i ]; LeftChild( i ) < N; i = Child )
{
Child = LeftChild( i );
if( Child != N - 1 && A[ Child + 1 ] > A[ Child )
Child++;
if( Tmp < A[ Child ] )
A[ i ] = A[ Child ];
else
break;
}
A[ i ] =Tmp;
}
KL University
12
EIGHT QUEENS PROBLEM
main( )
{
int i;
int Arr1[12]={100,23,45,24,37,78,90,1,71,33,11,29};
Heapsort( Arr1, 12);
for( i = 0 ; i<12; i++ )
{
printf("\t%d",Arr1[i]);
}
}
Output:
/*
C:\Users\Administrator\Desktop\stack>heapsort.exe
1
11 23
24
29
33
37
45
71
*/
Merge sort:
#include<stdio.h>
# include <stdlib.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf(%d,&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
78
90
100
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",merge[i]);
}
return 0;
KL University
13
EIGHT QUEENS PROBLEM
}
void partition(int arr[],int low,int high){
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else {
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
Output:
Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9
Module 4:
Quick sort:
#include <stdlib.h>
#include <stdio.h>
#define Cutoff ( 3 )
void Swap( int *Lhs, int *Rhs )
{
int Tmp = *Lhs;
*Lhs = *Rhs;
*Rhs = Tmp;
}
int Median3( int A[ ], int Left, int Right )
{
int Center = ( Left + Right ) / 2;
if( A[ Left ] > A[ Center ] )
Swap( &A[ Left ], &A[ Center ] );
If( A[ Left ] > A[ Right ] )
Swap( &A[ Left ], &A[ Right ] );
if( A[ Center ] > A[ Right ] )
Swap( &A[ Center ], &A[ Right ] );
/* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */
Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */
return A[ Right - 1 ];
/* Return pivot */
}
void InsertionSort( int A[ ], int N )
{
int j, P;
int Tmp;
KL University
15
EIGHT QUEENS PROBLEM
}
else /* Do an insertion sort on the subarray */
InsertionSort( A + Left, Right - Left + 1 );
}
main( )
{
int i;
int A[12]={100,23,45,24,37,78,90,1,71,33,11,29};
Qsort( A, 0,11);
for( i = 0 ; i<12; i++ )
{
printf("\t%d",A[i]);
}
}
KL University
16
EIGHT QUEENS PROBLEM
Output:
/*C:\Users\Administrator\Documents>"quick sort.exe"
1
11 23
24
29
33
37
45
71
*/
78
90
100
{
minmax.max = arr[0];
minmax.min = arr[1];
}
else
{
minmax.max = arr[1];
minmax.min = arr[0];
}
for (i = 2; i<n; i++)
{
if (arr[i] > minmax.max)
minmax.max = arr[i];
Output:
Minimum is 1
Maximum is 3000
Circular linked list:
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *insert(node *p, int n)
{
node *temp;
/* if the existing list is empty then insert a new node as the starting node */
if(p==NULL)
{
p=(node *)malloc(sizeof(node)); /* creates new node data value passes as parameter */
p-> data = n;
p-> next = p; /* makes the pointer pointing to itself because it is a circular list*/
}
else
{
temp = p;
/* traverses the existing list to get the pointer to the last node of it */
while (temp-> next != p)
temp = temp-> next;
{
node *temp;
temp = p;
printf("\nThe data values in the list are\n");
if(p!= NULL)
{
do
{
printf("%d\t",temp->data);
temp = temp->next;
}
while (temp!= p);
}
else
KL University
18
EIGHT QUEENS PROBLEM
19
EIGHT QUEENS PROBLEM
if (list->data == value)
{
previous->next = list->next;
free(list);
return first;
}
previous = list;
list = list->next;
}
//handle the last element
if (list->data == value)
{
previous->next = first;
free(list);
return first;
}
else
{
//if the element is not found
printf("The element %d has not been found in the list!\n", value);
return first;
}
}
void find(node *list, int value)
{
node *first = list, *previous = list;
if (list == NULL)
{
printf("The circularly linked list is empty!\n");
return;
KL University
20
EIGHT QUEENS PROBLEM
}
//handle the first element
if (list->data == value)
{
printf("The %d is found at the beginning of the list before %d!\n", value,
list->next->data);
return;
}
//handle the middle elements
while (list->next != first)
{
if (list->data == value)
{
printf("\n%d has been found between %d and %d\n", value, previous->data,
list->next->data);
return;
}
previous = list;
list = list->next;
}
//handle the last element
if (list->data == value)
{
printf("The \n%d is found at the end of the list after %d!\n", value, previous->data);
return;
}
printf("\nThe element %d is not found in the list!\n");
KL University
21
EIGHT QUEENS PROBLEM
return;
}
void main()
{
int n,j;
int x;
node *start = NULL ;
printf("Enter the nodes to be created \n");
scanf("%d",&n);
while (n-- > 0 )
{
printf( "Enter the data values to be placed in a node\n");
scanf("%d",&x);
start = insert ( start, x );
}
printf("The created list is\n");
printlist ( start );
printf ( "\n enter the element to remove from Linked List\n" ) ;
scanf("%d",&j);
start = removenode(start,j);
printlist ( start );
printf ( "\n enter the element to find from Linked List\n" ) ;
scanf("%d",&j);
find(start,j);
}
OUTPUT:
/*
C:\Users\Administrator\Desktop\stack>csll-final.exe
Enter the nodes to be created
4
Enter the data values to be placed in a node
1
Enter the data values to be placed in a node
2
KL University
22
EIGHT QUEENS PROBLEM
KL University
23
EIGHT QUEENS PROBLEM
5.1 REFERENCES: