Question

Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know w...

Can you explain how this code for the 8 Queens Problem works? It's in C++

In case you dont know what the 8 Queens problem is, "The eight queens puzzle is the problem of placing eightchess queens on an 8×8 chessboard so that no two queensthreaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal."

This is the code:

#include <iostream>

#include <cmath>

using namespace std;

bool ok(int q[], int c) {

    for (int i = 0; i < c; i++){

    if (q[i] == q[c] || abs(q[i] - q[c]) == c - i)

    return false;

    }

    return true;

}

void print(int q[]) {

    static int numSolutions = 0;

    cout << "Solution #" << ++numSolutions << ": ";

    for (int i = 0; i < 8; i++)

        cout << q[i];

    cout << "\n";

}

int main() {

    int q[8] = {0};

    int c = 0;

    while (c >= 0) {

    if (c == 7) {

    print(q);

    c--;

    }

    else q[++c] = -1;

    while (c >= 0) {

     q[c]++;

    if (q[c] == 8)

    c--;

    else if (ok(q, c))

        break;

    }

    }

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Eight Queen Puzzle 2mean, Thatnitionhuple 1lse else ok (1,1 ok 0 faheLast modified. 16.55

Add a comment
Know the answer?
Add Answer to:
Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know w...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

  • I have this code, but I need a flowchart that shows how my program works, and...

    I have this code, but I need a flowchart that shows how my program works, and the inputs and outputs??? #include <iostream> #include <cmath> using namespace std; int fact(int n) { int i; int c = 1; for(i=1; i<=n; i++) { if(n==0) c = 1; else c = c*i; } return c; } double ex(int n,double x) { int i; double c = 0.0; for(i=0; i<=n; i++) { c = c + (double)(pow(x,i)/ (double)fact(i)); } return c; } double sinx(int...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • I need a really good Pseudo/Algorithm code for this C++ program below. #include <iostream> #include<fstream> using...

    I need a really good Pseudo/Algorithm code for this C++ program below. #include <iostream> #include<fstream> using namespace std; int main() {    int low, high, i;//integer varaible    bool flag;//boolean flag    string a = "Enter two numbers(intervals): ";//string datatype    ofstream f;//fow writing to file    f.open("a.txt");    cout << a;    cin >> low >> high;    cout << "Prime numbers between " << low << " and " << high << " are: ";    do /*...

  • C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone...

    C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone please explain how we can get to that conclusion by just reading the code, can it be explained to me because when i try to do it I get a different answer for i and j I got 1 and 4. #include <iostream> using namespace std; int main() { int i = 1; int j = 0; if (i++ == 1) { j =...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT