tags:

views:

173

answers:

3

The Eight Queens puzzle is to place eight queens on a chessboard so that no two queens can attack each other(i.e. no two queen are on the same row, same column or same diagonal). There are many solutions and one of them is : solutionImage

The solution has to be using one dimensional array. Can someone help me out with this?

+2  A: 

If you have a one dimensional array you can use this method to use it as a bi-dimensional :

public Object getElement(int x, int y){
    return array[x+8*y];
}

From there you can use any method you want.

A good way to start is the fact that you know your queens can't be on the same line/column. From here you can easily do a relatively fast brute force algorithm.

Another way to start would the the usage of backtracking, you try to put your first queen on the first line/column, and you put the next queen to the nearest legal square, etc. Each time a queen can't be placed without breaking the rules, you go back and move your queen to the very next possible place.


Resources :

Colin Hebert
Depends how you want to iterate over it. But either is correct.
Justin K
Of course, there might want to be some bounds checking, perhaps extending to limiting the indices to the coordinate range; that is if you are logically dealing with an 8x8 plane, you might want to prohibit getElement(10,0), which would otherwise access element [1][2] in a 2 dimensional array where the first dimension is y and the second is x.
Software Monkey
@Software Monkey, It's a basic method. Of course you could add parameters checking and some exceptions. But I'm just giving help here, I'm not doing all the code :)
Colin Hebert
A: 

Well use one dimensional array and convert it the fly to 2D coordinate using modulo...

Loïc Février
+3  A: 

Use an array to store the row positions of the queens for each column, let's say a[], then to check if you can put a queen in column x, row a[x], you need to check:

  • That it's not in the same row that the previously placed queens: a[x] != a[i] for all i < x

  • It's not in the same diagonal as previously placed queens: a[x]+x != a[i]+i && a[x]-x != a[i]-i for all i < x

This is slower that using a two dimensional array, of course.

Ssancho
Seems like the only answer that got the point the of one-dimensional array, but I don't understand why this is slower than using two dimensional array?
Milo
To check if you can put a queen you need to loop trough all previous queens so see if there is a conflict, in the two dimensional array version you just check one array position.
Ssancho
how about check for same column?
I see. However, with the two dimensional array when you place a queen you would need to fill all the cells that are attacked by it. This would be slower than placing a queen with the one dimensional array. Overall performance may still be better than the one dimensional version, since the number of checks is higher than the number of actual placings. However, A faster implementation would just use several one dimensional arrays (for used columns, main diagonals, secondary diagonals), in order to combine the best of both approaches.
Milo
@user456527: that comes 'for free', you place one queen per column, for each step in the algorithm.
Ssancho
it suks i still can't figure out. what does 'i' suppose to mean..sori to bother, but cud u explain lil more?
'i' stands for all previous columns, so for example when x is 5 (you are putting the 6th queen), you check against the already placed queens in columns 0..4.
Ssancho