using namespace std;
//All the necessary functions
void nextRow(int[],int);
void backtrack(int[],int);
void print(int[],int);
bool ok(int[],int);
int main()
{
//This is the counter to effectively progress through the loop. the "flag" of the program.
int counter;
int q[8];
int c;
c = 0;
q[0] = 0;
counter = 2;
while(c > -1)
{
c++;
counter--;
if(c == 8){
print(q,c);
counter++;
}
if(counter == 1){
q[c] = -1;
nextRow(q,c);
}
}
return 0;
}
//This increments the row of the current column you are on, and if
the row is equal to 8, it knows to backtrack and try the next row in the
previous column.
void nextRow(int q[],int c)
{
q[c]++;
if(q[c] == 8)
backtrack(q,c);
ok(q,c);
}
//This is the test to see whether or not you can put a queen in that position.
bool ok(int q[],int c)
{
for(int i = 0; i < c; i++)
{
if(q[c] == q[i] || abs(q[c] - q[i]) == abs(c - i))
nextRow(q,c);
}
}
//Sets the current row equal to zero and goes back to the previous column
to increment the row.
void backtrack(int q[],int c)
{
q[c] = 0;
c--;
if(c == -1)
exit(0);
nextRow(q,c);
}
//Goes to the print function when c = 8.
void print(int q[],int c)
{
for(int i = 0; i < 8; i++)
{
cout << q[i];
}
cout << endl;
backtrack(q,c);
}
Any insight into this would be greatly appreciated for this aspiring C++ programmer :]. I want to know what I would have to change for it to work if the variables weren't global. I know it has to be something with the way I'm passing the variables through the functions, but I'm not sure. Thanks again guys.
No comments posted yet
Your Answer:
Login to answer
58
29
Other forums
PHP code for a proxy
I was wondering which function I could use to create an online proxy tool. I was thinking cURL would
Help If user voted, block them
On my Prayer request site, i let users Click a button to Pray for somone that has posted a pray, i g
What's wrong with my code
sobbing for many days, still don't know why please Help. Why my page isn't posted correctly. In a
Can I call a class inside a function?
I have a class written in another file that handles my image resizing.
Can I do this (php say
Using cURL to PUT
Can somebody help with the correct php code to make a cURL PUT request. Here is a sample of code bel
Help with ORDER BY
Hello. I would like to order by ascending States, then Cities, then Gyms in the following code, but
pagination - need help on passing of search query.
Hi, i have been trying for days but couldn't get this sorted out. Would like some professional help
Validation
Hello
I know this is a really vag question.
I need to have a input field on a form th
Multiple if statements
Ok so seems basic but for some reason I'm not doing it right. What I want is to be able to have 2 se
What do you call the "token" thing?
You know how some sites have links that run on tokens? Tokens are links that only stay alive for a c