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
Display search result
Hi!
I have a SQL database with information about albums and track (music).
This is wh
php require help needed
Ok i tried to use the search funtion but the word require is everywhere.
i'm really new to cr
Is it possible to view php source code??
Hi All
I was just wondering if it's possible to view a websites php source code?
I know
How a counter of users ? such as ---> (231 Viewing)
I want to count how many users are actually viewing the page, How is that possible?
Thank you guy
PHP Code reffering to database, doing something wrong.. pls help
Well i am quite new with PHP
and for school we have to make a PHP script which reffers to a Data
Having Problem with Date
l have a submit buttom where user have the possibility to add a date to specify when his message s
Inserting 100x and 100y coordinates in mysql table - easy
Is there a way to insert this data into a mysql table named 'map' with the fields: 'x int 3' , 'y in
Need help making a blockquote and line items conditional
I have some code I bought a few years ago that allows my clients to update content on their site usi
records between 2 dates
Hello all,
I have a date tracked table which contains effective_start_date and effective
Limiting uploaded file type
Hi. First post here
I am working on a simple upload script, and I need it to limit the allowed f