Ask C/C++ Expert


Home >> C/C++

Project

Your first project will be to write a C program to solve the n-Queens problem. The objective is to place n queens on a n × n chess board: no two queens are allowed to be in the same row, column, or a diagonal.

You can represent a board configuration with a simple n-element sequence Qn = {q0 q1 · · · qn-1}; qi corresponds to the column position of ith queen in row i. For example, {0 2 1 3} would correspond to the 4 queens in positions (0, 0) (row 0, column 0), (1, 2) (row 1, column 2), (2, 1) (row 2, column 1), and (3, 3) (row 3, column 3). Note that in our representation we do not use row indexes: the ith queen is placed in the ith row and its column index is qi.

To check whether a sequence Qn solves the n-Queens problem you need to make sure that

1. Two queens do not share a column, i.e. qi ≠ qj, for i ≠ j, and

2. Two queens do not share a diagonal, i.e. |qi - qj| ≠ |i - j|, when i ≠ j.

There are multiple ways to solve this problem. The simplest one would be to generate all possible configurations and check if they solve the problem. Note that there are nn possible configurations that would need to be checked. You can do better if you realize that you only need to check permutations of numbers 0, . . . , n-1 as possible configurations. (Why?) Note that in the case of the 4 queens that would be 256 possible board configurations, but there are only 24 permutations; you can verify that there are just 4 legal 4-queen placements. However, the method for generating all possible permutations is somewhat involved; it is much easier to generate random permutations.

Your assignment is to write a program to solve n-queens problem for n = 4, . . . , 20 using random board configurations. You should use your randperm function to do this. In addition, you will write two additional functions: checkboard for checking if a permutation solves the problem, and displayboard for printing a solution. Finally, for each board size you should solve the problem 10 times so that you can obtain some statistics on performance of your program.

Detailed requirements:

1. You will seed the random number generator using srandom(seed), where seed ={last 4 digits of your G#}.

2. You will write void randperm(int b[], int n) function to generate random boards. Note that you only need to initialize b[] once for each board size. A simple algorithm for generating a random permutation of elements of a chooses a random number 0 ≤ i ≤ n - 1 and swaps a[i] and a[n - 1]; this step is then repeated for the subsequence a[0 : n - 2].

You can use functions srandom() and random() to generate random numbers. srandom(1) will initialize your random number generator with seed 1, and k = random() will return a random long integer k. To produce a random number j such that 0 ≤ j < p you need a call j = random()%p. To use srandom() and random() you will have to include < stdlib.h >.

2. Write

int checkboard(int b[], int n)

checkboard returns 1 if the board represented by b[] solves n-queens problem, it returns 0 otherwise.

3. Write int displayboard(int b[], int n)

displayboard prints a solution of n-queens problem in an easy to check form. Here is a possible way your output should look for n = 6.

4. For each board size n = 4, . . . , 20 you will run your program 10 times to collect some statistics on its performance. For each n you will calculate min, max, and mean number of random boards generated until a solution was found. To calculate mean value for any board size you need to add up the total number of boards generated for that size and divide it by 10. To calculate the min and max values you can utilize the macros.

5. You will run your program for board sizes n = 4, . . . , 20 ten (10) times. You will display the first solution only for each value of n using displayboard. You will display the following statistics for each size n: min, max, and mean number of boards generated before a solution was found, nn and n!. You can use integers to calculate min and max values but you need to use floats or doubles to calculate the remaining three values.

Attachment:- Assignment File.rar

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92467201

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As