Hello all, sorry for this being my first post.

I have been dealing with this Nqueen program, and I cannot get it to work properly.

I feel like I have been just staring at my computer screen for hours and my brain is starting to hurt.

After entering n, the program just keeps asking for n, and does not procede. I am at a block and really not sure what is going on. I think I almost have this done!

Any insight would really be helpful, and I hope I can get this assignment done asap.

Am I even heading in the right direction? Should I be implementing this in a different way?

Thank you so much in advance.

HERE ARE THE INSTRUCTIONS:

Backtracking: Given: an NxN chessboard, each square described by a pair of coordinates (row,column) where row and column are numbers between 1 and N. A queen in chess can take a piece that is on the same row, column or diagonal. N queens can safely be placed on the board if no pair of queens occupies the same row, column or diagonal.
Write a program to determine the positions in which N queens may be placed on the board.
The input to the program is the number N. The output is a list of N positions. Each queen can be designated by a row number from 1 to N, since they must each be on different rows.

The following pseudocode describes an algorithm you may use. Assume that each queen is placed in a different column, starting with column=1

HERE IS MY CODE SO FAR:

#include <iostream>
#include <cmath>
#include <stack>
#include <vetor>
using namespace std;

class Queen
{
public:
 Queen();
 Queen(int row,int col);
 bool hit(Queen b, int size);
 int getrow();
 int getcol();
 int setcol(int x);

private:
 double row;
 double col;
};

Queen::Queen()
{
 this->row=1;
 this->col=1;
}

Queen::Queen(int row, int col)
{
 this->row=1;
 this->col=1;
}

bool Queen::hit(Queen b, int size)
{
 int r1=row;
 double r2=b.getrow();
 int c1=col;
 double c2=b.getcol();
 int rmod=abs(r2-this->row)
 int cmod=abs(c2-this->col);
//compare row
if (row==b.getrow())
  {return true;}
//compare col
if (col==b.getcol())
  {return true;}
//compare diag
if (rmod==cmod)
{return true;}

else
{return false;}
}

int Queen::getrow()
{return this->row;}

int Queen::getcol()
{return this->col;}

int Queen::setcol(int x)
{
 int x;
 this->col=x;
 return x;
}


int main()
{
 int n;
 int row;
 int col;
 cout << "Enter the num of queens: " << endl;
 cin>>n;
 Queen one(1,1);
 stack<Queen> final;
 final.push(one);
 
for (int 1=0;i<n;i++)
{
 Queen z(i,i);
 int c=z.getcol();
 if (one.hit(z,n)==true)
   {
    while(one.hit(z,n)==true)
     {
      int b=z.getcol();
      z.setcol((z.getcol()+1));
      one.hit(z,n);
     }
   final.push(z);
   }
 else
 {final.push(z);)
}

for (int i=1;i<n;i++)
{
 cout << (int)final.size()<<endl;
}

return 0;
}

Recommended Answers

All 3 Replies

/*I'm sorry the previous message was nothing but your program. Now I have found 8 dictionary mistakes in your program - you must be more careful - I fixed it and in my systems it runs OK. But if you wanted to explain what exactly this program do , I would be grateful..*/

#include <iostream>
#include <cmath>
#include <stack>
#include <vector>
using namespace std;

class Queen
	{
	public:
		Queen();
		Queen(int row,int col);
		bool hit(Queen b, int size);
		int getrow();
		int getcol();
		int setcol(int x);
		
	private:
		double row;
		double col;
	};

Queen::Queen()
{
	this->row=1;
	this->col=1;
}

Queen::Queen(int row, int col)
{
	this->row=1;
	this->col=1;
}

bool Queen::hit(Queen b, int size)
{
	int r1=row;
	double r2=b.getrow();
	int c1=col;
	double c2=b.getcol();
	int rmod=abs(r2-this->row);
	int cmod=abs(c2-this->col);
	//compare row
	if (row==b.getrow())
	{return true;}
	//compare col
	if (col==b.getcol())
	{return true;}
	//compare diag
	if (rmod==cmod)
	{return true;}
	
	else
	{return false;}
}

int Queen::getrow()
{return this->row;}

int Queen::getcol()
{return this->col;}

int Queen::setcol(int x)
{
	//int x;
	this->col=x;
	return x;
}


int main()
{
	int n;
	int row;
	int col;
	cout << "Enter the num of queens: " << endl;
	cin>>n;
	Queen one(1,1);
	stack<Queen> final;
	final.push(one);
	
	for (int i=0;i<n;i++)
	{
		Queen z(i,i);
		int c=z.getcol();
		if (one.hit(z,n)==true)
		{
			while(one.hit(z,n)==true)
			{
				int b=z.getcol();
				z.setcol((z.getcol()+1));
				one.hit(z,n);
			}
			final.push(z);
		}
		else
		{final.push(z);
		}
		
		for (int i=1;i<n;i++)
		{
			cout << (int)final.size()<<endl;
		}
		
		return 0;
	}
}

After entering n, the program just keeps asking for n, and does not procede. I am at a block and really not sure what is going on. I think I almost have this done!

I don't get... how did you run a non-compilable program?

I just put it in X-code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.