Write a C program to solve the n-Queens problem

Assignment Help C/C++ Programming
Reference no: EM131658499

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

Reference no: EM131658499

Questions Cloud

Analyzing and targeting the global market opportunity : Analyzing and Targeting the Global Market Opportunity - Global Market Segment, Global Targeting and Global Product Positioning
Discuss the clients stress and coping mechanisms : Discuss the client's stress and coping mechanisms. Are they healthy
Discuss an integrated corporate governance system : In this scenario, it is not clear that the other corporate officers and Tamik's board of directors were aware of the actions of its CEO, Arnett.
Various numbers of children age : The proportions of families with various numbers of children age 18 or under in a small town are given in the following table.
Write a C program to solve the n-Queens problem : CS 262 Project. Your first project will be to write a C program to solve the n-Queens problem. You will write void randperm(int b[], int n) function
Standard normal table to find the probability : Use the Standard Normal Table to find the probability. The lengths of pregnancies are normally distributed with a mean of 268 days and a standard deviation.
Draft paper with hierarchical structure of topics : Does the research deal with significant and meaningful problem that lends itself to a substantial research effort?
How would you summarize the federal rule of evidence : Research the Federal Rules of Evidence. How would you summarize the Federal Rule of Evidence 702 as it was initial passed by Congress in 1975?
Current employer''s decision to offer the compensation : What went into your previous or current employer's decision to offer the compensation and benefit plans it currently extends to employees?

Reviews

len1658499

9/26/2017 3:19:52 AM

Oh, I forgot to put some requirement. Don’t use global var and use sscanf() combination of Standard I/O functions if it has input. Instructions for submission: Use script do show your session in which you compile and run your code for various values of n. You should show testing of your functions. Use tar or zip command to create an archive of your script file, your fully commented source code, and your Makefile. All filenames should use the standard naming conventions that were used in the labs. Submit your tar or zip file on Blackboard.

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd