The beginner's 1D 8 Queens without goto program


For some reasons my code is not working correctly and it goes infinite loops, could anybody help me out please?

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
 #include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
bool okay(int q[], int col);
void backtrack(int & col); 
void print ( int q[]);

int main (){
	int solution = 0;
	int q[8]; q[0]=0; 
	int c = 0;
	bool from_backtrack = false;  

	while (true){
		while(c<8){
			if (!from_backtrack) { 
				c++;
				if (c==8) break;
				q[c] = -1;	
			} 
			   

				from_backtrack = false;
				while(q[c]<8){ 			
					q[c]++;
					if (q[c]==8) backtrack(c);
					if (okay(q,c) == true) break;
			}
		
		}
		solution ++;
		cout << "solution#" << solution << ": " ;
		print(q);                     
		backtrack(c);
		from_backtrack = true ;
	}
}
bool okay(int q[], int col){
		for ( int i = 0; i < col; i++){
			if (q[i]==q[col]||((col-i)==abs(q[col]-q[i]))) return false;
		else return true;
	}
}
void backtrack(int &col){
	col --;
	if (col == -1)
	exit(1);
}
void print (int q[]){
	for(int x =0; x<8; x++){
		cout << q[x] << " " ;
	}
	cout<< endl;
		
}
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
 #include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
bool okay(int q[], int col);
void backtrack(int & col); 
void print ( int q[]);

int main (){
	int solution = 0;
	int q[8]; q[0]=0; 
	int c = 0;
	bool from_backtrack = false;  

	while (true){
		while(c<8){
			if (!from_backtrack) { 
				c++;
				if (c==8) break;
				q[c] = -1;	
			} 
			   

				from_backtrack = false;
				while(q[c]<8){ 			
					q[c]++;
					if (q[c]==8){ backtrack(c); from_backtrack=true;}////<--------
					if (okay(q,c) == true) break;
			}
		
		}
		solution ++;
		cout << "solution#" << solution << ": " ;
		print(q);                     
		backtrack(c);
		from_backtrack = true ;
	}
}
bool okay(int q[], int col){
		for ( int i = 0; i < col; i++){
			if (q[i]==q[col]||((col-i)==abs(q[col]-q[i]))) return false;
		//else return true;/////<-------------------
	}
        return true;////<--------------------
}
void backtrack(int &col){
	col --;
	if (col == -1)
	exit(1);
}
void print (int q[]){
	for(int x =0; x<8; x++){
		cout << q[x] << " " ;
	}
	cout<< endl;
		
}
Last edited on
Hello rance,

Welcome to the forum.

You have set the outer most while loop up as an infinite loop with no way to get out of it. Between lines 33 and 36 you could add an if statement, maybe check the value of solution and if it becomes larger than some given amount then break out of the loop. Or change the condition of the while loop so that it will run a finite number of times.

Hope that helps,

Andy
You have set the outer most while loop up as an infinite loop with no way to get out of it.


The path out of the loop is through the exit statement in the backtrack function.
@vin,

Check the value of c @line 35 and also col @line 47 and 48. You should find that neither one will reach -1, so the exit statement is never reached and you have an endless while loop. I also notice that the program will print six lines with the only difference being the last number then it repeats. With out some way to limit the while loop it is an endless loop.

The last time I let the program run the solution# went above 10000 before I stopped it.

Andy
Topic archived. No new replies allowed.