Coding Eight Queens 2 Dimensional Problem

Hey i have been trying to code the eight queens 2 dimensional array for awhile but i cant seem to get it correctly, please i will need some assistance from you intelligent minds, thank you. heres what ive done so for


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 <iostream>
using namespace std;

int main (int argc, char * const argv[]) {
    // insert code here...
    int b[8][8]={0},r,c=0,i;
    b[0][8]=1;
Nc: c++;
	if (c==8){
		goto print;
	}
	r=-1;
Nr: r++;
	if (r==8) {
		goto backtrack;
	}
	// row conflict
	for (i=0; i < c; i++) {
		if (b[r][i] == 1) 
			goto Nr;
	}
	// up diagonal test
	for (i=1; (r-i>=0) && (c-i>=0); i++) {
		if (b[r-i][c-i]==1)
			goto Nr;
	}
	// down diagonal test
	for (i=1; (r-i)<=8 && (c-i) >=0; i++) {
		if (b[r+i][c-i]==1) 
			goto Nr;
		b[r][c]=1;
		goto Nc;
	}
backtrack: c--;
	//find the queen
	if (c==-1) {
		return 0;
	}
	while (b[r][c]!=1) {
		r++;
		b[r][c]=0;
		goto Nr;
	}
	//Print
print: b[r][c];
	cout << b[r][c];
	goto backtrack;
	return 0;
}

The output just exit the whole program, not sure whats going on, so please can anyone help me to fix it.
I'm not sure what's going on either, but I do know that this algorithm should be implemented with a recursive function and yours does not have one, thus backtracking is not possible. Try googling for solutions to this problem. There are plenty of them. Also, don't use gotos.
First it helps to spell out what the Eight Queens problem is, for those of you who haven't played with this puzzle yet, it is a chess puzzle, the objective of which is to place eight Queens on a chess board so that none of them are in a possition to capture another.

Now are you writing this in order to figure the puzzle out? Or are you assumed to already know the algorythm?
well the professor gave us this outcome and just said to complete it so everything up to print he gave and the rest we had to complete.
everything up to print he gave

What do you mean by this? Your professor gave you this code?
Hard to read


Try giving your variables more descriptive names, i know while codeing it may be a good idea temporarily just to get you idea into code, but then go back and rename them so you can read them easier.

Ive found it makes codeing alot easier for all
he coded it in class and said we should complete it, he coded up to where it says Print :
Maybe he's an assembly programmer and this course got thrust on him? Regardless, this person has no right to be teaching C++.

This is an array out of bounds error, btw:

b[0][8]=1;

For each dimension, the indeces should be in the range 0 to 7, inclusively.
Last edited on
Alright ive fixed some stuff but it says print not declared? where am i suppose to declare it?


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 <iostream>
using namespace std;

void EightQueens2D () {
    // insert code here...
    int b[8][8]={0},row,col=0,i;
    b[0][0]=1;
Nc: col++;
	if (col==8){
		goto print;
	}
	row=-1;
Nr: row++;
	if (row==8) {
		goto backtrack;
	}
	// row conflict
	for (i=0; i < col; i++) {
		if (b[row][i] == 1) 
			goto Nr;
	}
	// up diagonal test
	for (i=1; (row-i>=0) && (col-i>=0); i++) {
		if (b[row-i][col-i]==1)
			goto Nr;
	}
	// down diagonal test
	for (i=1; (row-i)<=8 && (col-i) >=0; i++) {
		if (b[row+i][col-i]==1) 
			goto Nr;
		b[row][col]=1;
		goto Nc;
	}
backtrack: col--;
	//find the queen
	if (col==-1) {
		return;
	}
	while (b[row][col]!=1) {
		row++;
		b[row][col]=0;
		goto Nr;
	}
	//Print
print: 
	print(b);
	cout << endl;
	goto backtrack;
}

You'll need to create your own function for that:

1
2
3
4
5
6
7
void print(int b[][8])
{
    ...
}

void EightQueens2D() {
   ...


Though you may want to call it something different because there's already a "print" label.
Topic archived. No new replies allowed.