Click here to Skip to main content
12,451,089 members (53,375 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
currently i am trying to allow the user to place his own set of 8 queens and allows user to backtrack. i cannot however get the queens to be placed on the board and haven't started the isSafe algorithm. if i can get some direction that would be nice thank you. first googled the problem but i'm trying to attempt this on my own first, don't want to be a lazy programmer and just have the answers given to me that would defeat the reason why i'm in college.
#include <iostream>
using namespace std;
 
void printGrid(char grid[][8], int w, int h);
void printQueen(char grid[][8], int w, int h);
 
int main(){
	//create a grid 8x8
	char grid[8][8];
	//loop for row
	for (int count =0; count<8;count++)
	{
		//colums 
		for (int count1 =0; count1<8;count1++)
	{
		grid[count][count1];
		}
	}
	//print grid
	
	printGrid(grid,8,8);
	
	system("pause");
	return 0;
}
void printGrid(char grid[][8],int w, int h)
{
	for (int i =0;i<w;i++)>
	{
		for (int j =0;j<h;j++)>
		{
			
			cout<<grid[i][j]<<'_';
		}
		
		cout<<endl;
	}
	printQueen(grid,8,8);
}
void printQueen(char grid[][8], int w, int h)
{
	int j,k;
 
	for (int i =0;i<7;i++)
	{
	//asks user for cooridinates to place queen doesnt check if queen is safe
      cout<<"enter the cooridinates for the queen (must be integer values between 1-8):"<<endl;
	  cout<<"enter row then colum : ";
	  cin>>j>>k;
	  cout<<grid[j][k]<<'Q';
	  printGrid(grid,8,8);
	}
	
	
}</iostream>
Posted 25-Feb-14 7:05am
Updated 25-Feb-14 7:08am
CHill60157.4K
v2
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 13:44pm
   
Your attitude is very good, but still, this is not a question. And you don't properly explain anything.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First of all, what are you attempting in the nested loops in the main()?
The line
grid[count][count1];
doesn't do anything.
Did you intend to initialize the array:
grid[count][count1] = ' ';
I'm not sure your printQueen routine is actually what you want.

Instead of starting with printing the board,
start with solving the problem.

Think about how you would do it manually:
Get (or draw) a chessboard (8x8 grid)
get 8 markers to represent the queens.
Get some markers to represent HOW MANY queens are attacking a position.
(In the US, I'd use 8 Quarters and a lot of pennies.)
Place a "queen" in the first position of the first row and then place a marker in every position that the queen could attack.
Place a second queen in the first open position of the second row.
Add a marker to the positions the second queen could attack. Some positions will have 2 markers!
repeat adding queens until you've placed the 8th queen or there are no open positions.
If there are no open positions, remove the last placed queen, remove the corresponding markers from that queen, and place the queen in the next available position.
Keep placing and removing queens and markers until the 8th is placed.

Now try to implement this in code.
Think about what the grid data structure needs to be.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The main problem you seem to be having is being clear about the sequence of events: you have elements of the things that need to be done in your program, but you're putting on socks after the shoes, if you get my meaning.

I strongly suggest you look at the problem definition and describe it in just a few (complex) steps, that need to be performed one after the other. Each of those steps represents a function that you will need to write. Then proceed to each function and do the same: describe them in somewhat smaller steps. After a few iterations you'll arrive at steps that can be put into program code directly.

Then you do the same for the data structures: looking at the function definitions, consider what information you need to pass to each function. You may find that some don't require nearly as much information as are inclined to pass along. And you may be able to recognize data elements that always need to be passed together, and would be advantagous to group in a data structure.

In your case, your purpose is to
1) read queen positions from user input
2) evaluate whether the position solves the problem
3) allow the user to backtrack
4) repeat from step 2)
5) print positions

The above steps are what your main function should look like. Step 4 implies a loop of some kind. You should consider how to exit that loop, and make sure that you don't remain stuck within. The other steps are just function calls.

Step 1): Why you call the function for reading the positions from user input "printQueen" is a mystery to me. Apart from that, this function shows a couple of problems:
- what is the purpose of the arguments w and h?
- how many iterations does your for loop make?
- do you actually need two input coordinates?
- do you actually need three variables (i, j, k)?
- why do you print the grid after every single input?
- why do you print it at all? This is not the function for printing the grid, it's the function for reading positions!
- what is the value of grid[0][0], grid[0][1], etc. after user input?

Step 2): As you said, you still need to write that function

Step 3): is somewhat vague - what exactly should the user be allowed to change when 'backtracking'? The answer determines how you go about communicating with the user. More importantly, how do you refer to the positions currently stored? How do you erase them when changed?

Step 4): again you are having issues for correctly dealing with dimensionality information.
- you are using parameters w and h, but then the second dimension of grid is indicated as 8. This doesn't make sense: if h were greater than 8, you'd read past the end of the array.
- the call to printQueen() doesn't use h and w?
- why do you even call printQueen()? This is not the purpose of this function!

Some of the issues listed above don't have trivial answers. Feel free to ask if you can't come up with a solution to any individual point.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 26 Feb 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100