8 queens problem conflict check

Hello all, I recently read up about the 8 queens problem and tried to solve it myself, in C++. I made a class called eq.h, an implementation file called eq.cpp, and a main.cpp.

The thing that I'm not sure about is, how do I check for row, column, and diagonal conflicts? What I'm thinking is, to use a nested for loop for conflict checking. How can that be applied? I've created a valid() function for that purpose. Below is what I've got so far.

1)eq.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef 8QUEEN_H
#define 8QUEEN_H

#include <iostream>
#include <algorithm>

using namespace std;

class 8queen
{
        public:
            8queen();
            ~8queen();
            int solve();//solve problem using next_permutation
            void display();

        private:
            bool valid();
            int queens[8]; //array to store 8 integers that represent 8 queens
};
#endif 


2) eq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "eq.h"
#include <iostream>
#include <algorithm>

using namespace std;

8queen::8queen()
{
    for(int i=0;i<0;i++)
    {
        queens[i]=i;
    }
}

8queen::~8queen()
{

}

//using next_perm, return all possible positions of the queens
int 8queen::solve()
{
    int count=0;
    do{
        if(valid())
        {
            count++;
            display();
        }
    }
    while(next_permutation(queens,queens+8));
    return count;
}

//display the positions of the queens
void 8queen::display()
{
    for(int i=0; i<0;i++)
    {
        cout<<queens[i]<<' ';
    cout<<endl;
    }
}

//check if position is valid or not
bool 8queen::valid()
{
    return true;
}


3) main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "eq.h"
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    8queen object;


    cout<<object.solve()<<endl;
}


I'd like to get some help on this, thanks.
Use the instructions found here (Algorithm):
http://www.math.utah.edu/~alfeld/queens/queens.html

Also take a look at the for loops in your code. They don't do anything

Topic archived. No new replies allowed.