You are on page 1of 32

1

EIGHT QUEENS
PROBLEM

CHAPTER 1

1.1 PROJECT DESCRIPTION:


The eight queens puzzle is the problem of placing eight chess queens on an 88 chessboard
so that no two queens threaten each other. Thus, a solution requires that no two queens share
the same row, column, or diagonal.
Description 1 what is eight queens puzzle deals about?
Using a regular chess board, the challenge is to place eight queens on the board such that no
queen is attacks any of the others. (For those not familiar with chess pieces, the queen is able
to attack any square on the same row, any square on the same column, and also any square on
either of the diagonals).
That is the all possible moves of a queen over a 8X8 board would be as shown in the
followed diagram:
The eight queens puzzle has 92 distinct solutions. If solutions that differ only by symmetry
operations (rotations and reflections) of the board are counted as one .

KL University

Department of Computer Science And Engineering

2
EIGHT QUEENS PROBLEM

The above fgure gives about the detais and information about the 12 fundamental
solutions.

1.2 MODULE WISE DESCRIPTION OF PROJECT:


This project has been segregated into four corresponding modules:
Module 1:
1. The first module deals about the matrix way of solving the corresponding given
eight queen and nights problem. In this the arrangement should be like no queen
should capture another queen.
2. That is it gives valid information regarding the board size, it also returns the position
of chess queens on the game board.
Module 2:
1. This module mainly deals about the solving the N queen problem using Backtracking. It
returns false if queens cannot be placed , otherwise returns true and returns placement of
the queens.
2. The additional feature involved over here is that it provides more than one solutions .
Module 3:
1. This module deals about the usage of heap sorting for sorting the elements depending
upon the moves, in this along with it another sorting method merge sort is being used in
order to sort the elements in the desired fashion.
Module 4:
This module completely deals about the internal processing of the program using quick sort
and multilinked list.

KL University

Department of Computer Science And Engineering

3
EIGHT QUEENS PROBLEM

CHAPTER 2

2.1 PSUEDO CODE:


The overall pseudo code of the programme can be minimized as:
int PlaceQueen(int board[8], int row)
If (Can place queen on ith column)
PlaceQueen(newboard, 0)
Else
PlaceQueen(oldboard,oldplace+1)
End
General structure of a Solution using Backtracking:
public ... backtrackSolve("problem N")
{
if ( "problem N" is a solved problem )
{
print "(solved) problem N"; // It's a solution !
return;
}
for ( each possible step S you can make in "problem N" )
{
if ( step S is a legal move )
{
Make step S;
backtrackSolve("problem N-1");
Take step S back;
}
}
}

KL University

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

5
EIGHT QUEENS PROBLEM

for(i=0; i<row; ++i) { // for all rows before this row.


markedCol = getMarkedCo1 (a, n, i );
if(markedCo1 = col || abs(row-i) = abs(co1 -markedCol))
return FALSE ;
}
return TRUE;
}
void NQueens(int a[][N], int n, int row) {
/*
* solve n-queens problem. the solution is obtained using matrix a.
* the procedure is recursive. current row being considered is row.
* that means all the rows from 0 to row-1 are considered.
*/
int j;
if(row < n) {
for(j=0; j<n; ++j) // for each col.
if(feasible(a, n, row, j)) {
a[row][j] = TRUE;
NQueens (a, n, row+l);
a[row][j] = FALSE;
}
}
else
printMatrix(a, n);
}
int main() {
int a[Nl[Nl ; int
i=0, j=0; for(i=0;
i<8; i++) for(j=0;
j<8; j++) a[i]
[j]=0; NQueens(a,
8, 0);
return 0;
}
Output:
Please enter the board size: 4
Place queens in following positions:
Row 1-Col 2: | Q | | |
Row 2-Col 4: | | | Q |
Row 3-Col 1: Q | | | |
Row 4-Col 3: | | Q | |
We hope you enjoyed Eight Queens.

KL University

Department of Computer Science And Engineering

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");
}
}

bool isSafe(int board[N][N], int row, int col)


{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false
for (i=row, j=col; i>=0 && j>=0; i--, j--)
if (board[i][j])
return false;
for (i=row, j=col; j>=0 && i<N; i++, j--)
if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[N][N], int col)
KL University

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

8
EIGHT QUEENS PROBLEM

printSolution(board);
return true;
}

// driver program to test above function


int main()
{
solveNQ();
return 0;
}

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

Department of Computer Science And Engineering

9
EIGHT QUEENS
PROBLEM

printf("Enter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array is :");


for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);

//left recursion mergesort(a,mid+1,j);

//right recursion merge(a,i,mid,mid+1,j);

//merging of two

sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50];

//array used for merging

int i,j,k;
KL University

Department of Computer Science And Engineering

10
EIGHT QUEENS PROBLEM

i=i1;

//beginning of the first list

j=i2;

//beginning of the second list

k=0;
while(i<=j1 && j<=j2)

//while elements in both lists

{
if(a[i]<a[j])
temp[k++]=a[i++];
else temp[k++]=a[j+
+];
}

while(i<=j1)

//copy remaining elements of the first list

temp[k++]=a[i++];

while(j<=j2)

//copy remaining elements of the second list

temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Output:
Enter number of elements = 4
Enter array elements: 2 44 78 55
Sorted array is: 2 44 55 78.

KL University

Department of Computer Science And Engineering

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;
}

void Heapsort( ElementType A[ ], int N )


{
int i;
for( i = N / 2; i >= 0; i-- ) /* BuildHeap */
PercDown( A, i, N );
for( i = N - 1; i > 0; i-- )
{
Swap( &A[ 0 ], &A[ i ] ); /* DeleteMax */
PercDown( A, 0, i );
}
}

KL University

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

15
EIGHT QUEENS PROBLEM

for( P = 1; P < N; P++ )


{
Tmp = A[ P ];
for( j = P; j > 0 && A[ j - 1 ] > Tmp; j-- )
A[ j ] = A[ j - 1 ];
A[ j ] = Tmp;
}
}
Void Qsort( int A[ ], int Left, int Right )
{
int i, j;
int Pivot;
if( Left + Cutoff <= Right )
{
Pivot = Median3( A, Left, Right );
i = Left; j = Right - 1;
for( ; ; )
{
while( A[ ++i ] < Pivot ){ }
while( A[ --j ] > Pivot ){ }
if( i < j )
Swap( &A[ i ], &A[ j ] );
else
break;
}
Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */
Qsort( A, Left, i - 1 );
Qsort( A, i + 1, Right );

}
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

Department of Computer Science And Engineering

16
EIGHT QUEENS PROBLEM

Output:
/*C:\Users\Administrator\Documents>"quick sort.exe"
1
11 23
24
29
33
37
45
71
*/

78

Finding Maximum and Minimum:


/* structure is used to return two values from minMax() */
#include<stdio.h>
struct pair
{
int min;
int max;
};
struct pair getMinMax(int arr[], int n)
{
struct pair minmax;
int i;
/*If there is only one element then return it as min and max both*/
if (n == 1)
{
minmax.max = arr[0];
minmax.min = arr[0];
return minmax;
}
/* If there are more than one elements, then initialize min
and max*/
if (arr[0] > arr[1])

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];

else if (arr[i] < minmax.min)


minmax.min = arr[i];
}
return minmax;
}
/* Driver program to test above function */
int main()
{
int arr[] = {1000, 11, 445, 1, 330, 3000};
int arr_size = 6;
struct pair minmax = getMinMax (arr, arr_size);
printf("\nMinimum element is %d", minmax.min);
printf("\nMaximum element is %d", minmax.max);
}

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;

temp-> next = (node *)malloc(sizeof(node));


/* creates new node using data value passes as parameter and puts
its address in the next field of last node of the existing list*/
temp = temp-> next;
temp-> data = n;
temp-> next = p;
}
return (p);
}
void printlist ( node *p )

{
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

Department of Computer Science And Engineering

18
EIGHT QUEENS PROBLEM

printf("The list is empty\n");


}
node *removenode(node *list, int value)
{
node *first = list, *last = list, *previous = list;
if (list == NULL)
{
printf("The circularly linked list is already empty!\n");
return;
}
//handle the first element of the list
if (list->data == value)
{
//handle only one element in the list
if (list->next == list)
{ free(list);
return NULL;
}
//get the last element of the list
while(last->next != first) last = last->next;
//connect last element with 2nd element
first = first->next;
last->next = first;
free(list);
return first;
}
//handle the middle elements
while (list->next != first)
{
KL University

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

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

Department of Computer Science And Engineering

22
EIGHT QUEENS PROBLEM

Enter the data values to be placed in a node


3
Enter the data values to be placed in a node
4
The created list is
The data values in the list are
1234
enter the element to remove from Linked List
1
The data values in the list are
234
enter the element to find from Linked List
3
3 has been found between 2 and 4
*/

KL University

Department of Computer Science And Engineering

23
EIGHT QUEENS PROBLEM

5.1 REFERENCES:

DATA STRUCTURES:A PSUEDO CODE APPROACH WITH C-SECOND


EDITION BY RICHARD F.GILBERG,BEHROUZ A.FOROUZAN.
ALFRED V. AHO, JEFFREY D. ULLMAN, JOHN E. HOPCROFT. DATA
STRUCTURES AND
ALGORITHMS ADDISON WESLEY, 1983.

SOCIAL NETWORKING SITES:


https://www.wikipedia.google

You might also like