Question

It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...

It is N queens problem please complete it
use this code

//***************************************************************
// D.S. Malik
//
// This class specifies the functions to solve the n-queens
// puzzle.
//***************************************************************

class nQueensPuzzle
{
public:
nQueensPuzzle(int queens = 8);
    //constructor
    //Postcondition: noOfSolutions = 0; noOfQueens = queens;
    // queensInRow is a pointer to the array
    // that store the n-tuple.
    // If no value is specified for the parameter queens,
    // the default value, which is 8, is assigned to it.
bool canPlaceQueen(int k, int i);
    //Function to determine whether a queen can be placed
    //in row k and column i.
    //Postcondition: returns true if a queen can be placed in
    // row k and column i; otherwise it returns false

void queensConfiguration(int k);
    //Function to determine all solutions to the n-queens
    //puzzle using backtracking.
    //The function is called with the value 0.
    //Postcondition: All n-tuples representing solutions of
    // n-queens puzzle are generated and printed.

void printConfiguration();
    //Function to output an n-tuple containing a solution
    //to the n-queens puzzle.

int solutionsCount();
    //Function to return the total number of solutions.
    //Postcondition: The value of noOfSolution is returned.

private:
int noOfSolutions;
int noOfQueens;
int *queensInRow;
};



#include <iostream>
#include <cmath>
#include "nQueenPuzzle.h"


using namespace std;

nQueensPuzzle::nQueensPuzzle()
{
    noOfQueens = 8;
    queensInColumn = new int[8];
    noOfSolutions = 0;
}

nQueensPuzzle::nQueensPuzzle(int queens)
{
    noOfQueens = queens;
    queensInColumn = new int[noOfQueens];
    noOfSolutions = 0;
}

bool nQueensPuzzle::canPlaceQueen(int k, int i)
{
    for(int j = 0; j < k; j++)
        if((queensInColumn[j] == i)
            || (abs(queensInColumn[j] - i) == abs(j-k)))
            return false;
    return true;
}

void nQueensPuzzle::queensConfiguration(int k)//, int queens)
{
    for(int i = 0; i < noOfQueens; i++)
    {
        if(canPlaceQueen(k, i))
        {
            queensInColumn[k] = i;
            if(k == noOfQueens - 1)
                printConfiguration();
            else
                queensConfiguration(k + 1);
        }
    }
}

void nQueensPuzzle::printConfiguration()
{
    noOfSolutions++;
    cout<<"(";
    for(int i = 0; i < noOfQueens - 1; i++)
        cout<<queensInColumn[i]<<", ";


    cout<<queensInColumn[noOfQueens - 1]<<")"<<endl;
}

int nQueensPuzzle::solutionsCount()
{
    return noOfSolutions;
}

Homework Answers

Answer #1

Please give positive ratings for my efforts. Thanks.

PROGRAM

#include<stdio.h>
#include<math.h>

int board[20],count;
 
int main()
{
 int n,i,j;
 void queen(int row,int n);
 
 printf(" - N Queens Problem Using Backtracking -");
 printf("\n\nEnter number of Queens:");
 scanf("%d",&n);
 queen(1,n);
 return 0;
}
 
void print(int n)
{
 int i,j;
 printf("\n\nSolution %d:\n\n",++count);
 
 for(i=1;i<=n;++i)
  printf("\t%d",i);
 
 for(i=1;i<=n;++i)
 {
  printf("\n\n%d",i);
  for(j=1;j<=n;++j)
  {
   if(board[i]==j)
    printf("\tQ"); 
   else
    printf("\t-");
  }
 }
}
 
int place(int row,int column)
{
 int i;
 for(i=1;i<=row-1;++i)
 {
  if(board[i]==column)
   return 0;
  else
   if(abs(board[i]-column)==abs(i-row))
    return 0;
 }
 
 return 1;
}
 
void queen(int row,int n)
{
 int column;
 for(column=1;column<=n;++column)
 {
  if(place(row,column))
  {
   board[row]=column; 
   if(row==n) 
    print(n);
    else
    queen(row+1,n);
  }
 }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
I am trying to print out an 8 by 8 "chess board". My first step is...
I am trying to print out an 8 by 8 "chess board". My first step is to set all values in the board to 0. I made a function to do so but it isn't working, and i can't figure out why. I'm trying to, for now, simply print out an 8 by 8 of zero values. This is what I have: (C++) // set all of board to 0 as a function // bool function to check for valid...
I need to use a Bubble Sort to sort data: “C”, “Y”, “B”, “N”, “Q”, “G”,...
I need to use a Bubble Sort to sort data: “C”, “Y”, “B”, “N”, “Q”, “G”, “C” – show count of “swaps” for each 8 Queens: Place initial “Queen” on row 6, column 1 *Knight’s Tour – initial position Row 1, Column 1, for the Queens question I just need the final position of the Queen
getting an error for the code below when attempting to compile . Not fully sure what...
getting an error for the code below when attempting to compile . Not fully sure what the error means and confused why I am getting the error. I know there are a of other logical errors but im just looking for the solution to this specific one right now. /tmp/ccq2rdty.o: In function `main': IntArrayPlay.cpp:(.text+0x42): undefined reference to `fillArray(int*, int&)' IntArrayPlay.cpp:(.text+0x55): undefined reference to `displayArray(int*, int&)' IntArrayPlay.cpp:(.text+0x114): undefined reference to `displayArray(int*, int&)' IntArrayPlay.cpp:(.text+0x15f): undefined reference to `displayArray(int*, int&)' collect2: error: ld...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
    asked 1 minute ago
  • How could your result from GC (retention time, percent area, etc.) be affected by these following...
    asked 11 minutes ago
  • QUESTION 17 What are the tasks in Logical Network Design phase? (Select five. ) Design a...
    asked 13 minutes ago
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 22 minutes ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 35 minutes ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 43 minutes ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 55 minutes ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 58 minutes ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 59 minutes ago
  • Using python, please explain. The factorial of an integer N is the product of the integers...
    asked 1 hour ago
  • alamoto Co. manufactures a single product that goes through two processes — mixing and cooking. The...
    asked 1 hour ago
  • QUESTION 21 _______ is the ease of use with which network users can access the network...
    asked 1 hour ago