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
Help Ordering Arrays
Hi, I have 3 arrays as shown below.
Code: $users = $this->get_latest_users();
$flir
a dificult string search
Hi I don't know a way around this. I want the user to input a password, but to make it a bit complca
Why does first ever HttpSendRequest take longer?
I promise this isn't as simple as it sounds. I'm wondering why the the first ever call to HttpSendRe
PHP Tab Control
Hi All,
I would like to have PHP tab control with/without Javascript.
But I want to r
Format String help
I have a textbox where a person enters an application number. the application number is 10 characte
Create comparison matrix?
I want to create a comparison script for forum software and was wondering is there any tutorial or b
how to hide input fields on selection screen using variant attribute
Hello all,
I want to know how to hide input fields on selection screen using variant attr
Parse Error
Hi Guys,
I have a function in my class which returns a string link variable. The problem is i
please help me in this update statment
hi every one
if I have table and this data in it
id name
10
Quick Syntax Question
Hi folks,
I'm getting the following error: "unexpected T_LNUMBER". I'm trying to b