// Dr. Peter Wang
// Data Structure and Algorithm
// CMP507
// Spring, 1997
// Eight queen problem

#include 
void find(void);
int nofsolution=0;
class eightqueen {
	int a[8];
	int b[15];
	int c[15];
	int x[15];
public:
	void init(void);
	void set(int i, int j);
	void reset(int i, int j);
	int  check(int i, int j);
	void locate(int i, int j);
	int  next(int i);
	void print(void);
};

eightqueen eq;
// =============================================== init
void eightqueen::init(void)
{
	int i;
	for(i=0;i<8;i++) eq.a[i] = 1;
	for(i=0;i<15;i++) eq.b[i] = eq.c[i] = 1;
}
// ============================================== set
void eightqueen::set(int i, int j)
{
	eq.a[j] = eq.b[i+j] = eq.c[i-j+7] = 1;
}
// ============================================== reset
void eightqueen::reset(int i, int j)
{
	eq.a[j] = eq.b[i+j] = eq.c[i-j+7] = 0;
}
// =============================================== next
int eightqueen::next(int i)
{
	return (eq.x[i]+1);
}
// ============================================= check
int  eightqueen::check(int i, int j)
{
	if(eq.a[j] && eq.b[i+j] && eq.c[i-j+7]) return 1;
	else return 0;
}
// ============================================ locate
void eightqueen::locate(int i, int j)
{
	eq.x[i] = j;
}
// ============================================ print
void eightqueen::print(void)
{
	int i;
	cout << nofsolution++ << " ===>  ";
	for(i=0;i<8;i++) cout << x[i] << '\t';
	cout << endl;
}
// =========================================== main
void main(void)
{
  	eq.init();
	find();
	{ char buf[80]; cin >> buf;}
}
// =========================================== find
void find(void)
{
	int found;
	int i=0;
	int j;
	int s=0;

 	for(;;) {
		found=0;
		for(j=s;j<8;j++) {
			if(eq.check(i,j)) {
				s=0;
				found=1;
				eq.locate(i,j);
				eq.reset(i,j);
				if(i<7) {i++; break;}
				else {eq.print();
				eq.set(i,j); 
				s=eq.next(i);
  				if(s>7) {i--;s=eq.next(i);eq.set(i,s-1);}
				}
			}
		}
		if(!found) {
 			i--;
			s=eq.next(i); eq.set(i,s-1);
 			if(s>7) {i--; s=eq.next(i);eq.set(i,s-1); } 
			if(i<0) return;
		}
	}
}

    Source: geocities.com/hsvfapa