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
whats wrong with my code please help!!!
this is the error
Warning: mysql_close(): supplied argument is not a valid MySQL-Link res
void* and sizeof()
Hi,
Using void* and sizeof is it possible to get the value of the object ?
I understan
Taking an HTML form and a PHP program and making it into one working file.
Hello,
So I would like to display everything INSIDE a specific directory. In other words, I
need to add "sizes" to shopping cart
Hey guys, I am trying to figure out a way to add a "size" selector on to this bit of code.
Extra fields in main body and php required.
I'm new to PHP so please be gentle. I have a little php script below that works well.
The &quo
preg-match with a string and numbers
Hi, I can't get the expression to work for my preg_match, what I'd like is for it to match the strin
Is there a quick way to do this?
Hi all,
I need to create a mysql table from php that goes a little something like this
Not loading image
When this function gets loaded it doesn't load the image just trying to figure out why.
Code:
Changing color in GD via variable?
Hey guys, I'm new here. x)
I'm very much a noob when it comes to PHP, but I'm trying to learn
cURL Upload Help
OK, I am creating a bridge from a local program to my website and I am trying to find the best way t