Help needed with 8 Queens Problem

I desperately need help understanding this program, especially lines 17-24. My programming teacher is rather adamant that I follow his method to solve the problem.


Output from the program consists of a one line per solution representation. Each solution will be sequentially numbered 1 . . . N. Each solution will consist of 8 numbers. Each of the 8 numbers will be the ROW coordinate for that solution. The column coordinate will be indicated by the order in which the 8 numbers are printed. That is, the first number represents the ROW in which the queen is positioned in column 1; the second number represents the ROW in which the queen is positioned in column 2, and so on.

Input: (Row number followed by column number)
1 1
Output:
SOLUTION #1: 1 5 8 6 3 7 2 4
SOLUTION #2: 1 6 8 3 7 4 2 5
SOLUTION #3: 1 7 4 6 8 2 5 3
SOLUTION #4: 1 7 5 8 2 4 6 3
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
  

#include <bits/stdc++.h>
using namespace std;
int row, col, row_posn[8], cnt=0,i;
bool r_exist[8]={false}, d1_exist[15]={false}, d2_exist[15]={false};

void search(int c) {
	if ( c==8 ) {
		cnt++;
		cout<<"SOLUTION #"<<cnt<<": ";
		for (i=0;i<8;i++){
		cout<<row_posn[i]+1<<" ";}
		cout<<endl;

	}
	if ( c==col ) search(c+1);
	else { 
	for (int r=0;r<8;r++){
		if (!r_exist[r]&& !d1_exist[r+c]&& !d2_exist[7+r-c]) {
			row_posn[c]=r;
			r_exist[r]=d1_exist[r+c]=d2_exist[7+r-c]=true;
			search(c+1);
			r_exist[r]=d1_exist[r+c]=d2_exist[7+r-c]=false;
		}
	}
	   
	   
	   
	} 
    // by default function always return here
}

int main(){
	
	cin >> row >> col;
    row--;
    col--;
    row_posn[col]=row;
    r_exist[row]=d1_exist[row+col]=d2_exist[7+row-col]=true;
      
	search(0);

	return 0;
}

Last edited on
Hi,
Does this assignment contain output samples?
What problems are you trying to solve?
What is your current program output now?
Last edited on
Hi, this is a sample input and output
Input: (Row number followed by column number)
1 1
Output:
SOLUTION #1: 1 5 8 6 3 7 2 4
SOLUTION #2: 1 6 8 3 7 4 2 5
SOLUTION #3: 1 7 4 6 8 2 5 3
SOLUTION #4: 1 7 5 8 2 4 6 3
The current program output is accurate, and this program is the answer my teacher provided. However, I do not understand the program.
We are supposed to learn Complete Search and recursive backtracking from this assignment but I am not sure how these techniques are employed
Last edited on
closed account (48bpfSEw)
demonix: "However, I do not understand the program."

Is there any word or symbol in the code which you don't understand?

It is hard to unterstand a big concept like an already written solution. Break down this big problem into little pieces until you understand one of this pieces, then find the next piece you can unterstand, then find the next one, and so on... finally you'll unterstand the hole.
An other learning technique is to draw the problem and every single step the programm runs through with its parametervalues.

This is very important : find your miss-understoods! This is the real problem!

For example: did you know what this symbol means: >> or this one [ ]?
Hi Necip, thanks for your hep. I do not understand these few statements despite trying to substitute values and drawing out the problem.
1
2
3
4
5
6
7
8
for (int r=0;r<8;r++){
		if (!r_exist[r]&& !d1_exist[r+c]&& !d2_exist[7+r-c]) {
			row_posn[c]=r;
			r_exist[r]=d1_exist[r+c]=d2_exist[7+r-c]=true;
			search(c+1);
			r_exist[r]=d1_exist[r+c]=d2_exist[7+r-c]=false;
		}
	}
closed account (48bpfSEw)
I don't know, ask your teacher! He should comment his solution so that everyone can understand his concept. Good code is nothing if no one can unterstand!

What could

1
2
3
r_exist  [8  ]={false}, 
d1_exist[15]={false}, 
d2_exist[15]={false};


mean?

Topic archived. No new replies allowed.