Dumb 8 Problem using single array

Hi everyone. I have been trying to understand what is wrong with my program. It is the dumb 8 queens problem where we have to place 8 queens on a chess board while making sure they dont kill each other off. My program using backtracking works perfectly but the dumb 8 version does not. Here is my code

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>    
#include<cstdlib>    
#include <cmath>    
using namespace std;    

bool ok(int q[]){						//single array q
	for(int col = 7; col >= 0; col--){	//for columns 7 to 0, decrement
		for(int i=0; i<col; i++)		//i from 0 to value of column, increment
		if(q[col]==q[i] || (col-i)==abs(q[col]-q[i])) //row and diagonal test
			return false;
return true;
}
}

void print(int q[]){
	static int count = 0;
	
	cout << ++count << endl;
	
{
for (int j=0;j<8;j++)
cout << q[j] << " ";				//print array q
}
cout << endl;
}


int main ()

{
	int q[8] = {0};

	for(int i0 = 0; i0 < 8; i0++)				
       for(int i1=0; i1 < 8; i1++)
          for(int i2 = 0; i2 < 8; i2++)
			for(int i3 = 0; i3 < 8; i3++)
				for(int i4 = 0; i4 < 8; i4++)
					for(int i5 = 0; i5 < 8; i5++)
						for(int i6 = 0; i6 < 8; i6++)
							for(int i7 = 0; i7 < 8; i7++){

									
										q[0]=i0;
										q[1]=i1;
										q[2]=i2;
										q[3]=i3;
										q[4]=i4;
										q[5]=i5;
										q[6]=i6;
										q[7]=i7;

								if(ok(q)) print(q);
									 
										q[0]=0;
										q[1]=0;
										q[2]=0;
										q[3]=0;
										q[4]=0;
										q[5]=0;
										q[6]=0;
										q[7]=0;

										
							}		
    
							
	return 0;
}


It should give me 92 solutions but it is producing an infinite loop and the answers are obviously not correct. Please help me.

Thanks!
The loop has no reason to be infinite. It's just that 810/2 cycles will take a while.
Also, I don't see an error in ok(). What kind of wrong results did it produce ?

To make this brute thing faster (but still ridiculously slow), notice that of all combinations of 8 numbers 0 to 7, only the ones that have no repetitions interest you. These combinations are permutations of 01234567. Look into http://www.cplusplus.com/reference/algorithm/next_permutation/
Topic archived. No new replies allowed.