Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking 2-dimensional array (like eight queens puzzle)

My problem is very similar to eight queens puzzle.

I've got 2-dimensional array (N x N) that for example, looks like this:

0,0,0,0,1 y
0,0,0,0,0 |
0,0,0,0,0 V
0,0,0,1,0
0,0,0,0,0
x->

I'm checking horizontally, vertically and diagonally for occurrences of 1

\,0,|,0,/
0,\,|,/,0
-,-,1,-,-
0,/,|,\,0
/,0,|,0,\

I'm thinking about storing only the (x,y) postions of "1"'s in a list

[[4,0],[3,3]]

and solving it mathematically, check every position of "1" with another (x1,y1)<->(x2,y2),

if x1 == x2 or y1 == y2 we have a collision! if not check:

x2 == x1 + z;
y2 == y1 + z;
x2 == x1 - z;
y2 == y1 - z;

(???)

where z is +/- that ( x1+z in 0..N ) and ( y1+z in 0..N ) .......

My problem is checking for diagonal collision, is there a better way to do it???

like image 234
M_1 Avatar asked Dec 21 '08 20:12

M_1


People also ask

Which algorithm is used to solve 8 queens problem?

To solve this problem, we will make use of the Backtracking algorithm. The backtracking algorithm, in general checks all possible configurations and test whether the required result is obtained or not. For thr given problem, we will explore all possible positions the queens can be relatively placed at.

How to solve n queens problem?

1) Start in the leftmost column 2) If all queens are placed return true 3) Try all rows in the current column. Do following for every tried row. a) If the queen can be placed safely in this row then mark this [row, column] as part of the solution and recursively check if placing queen here leads to a solution.

Is there symmetrical solution to the 8 queens problem?

3: The only symmetrical solution to the eight queens puzzle of Example 2.52 (except for rotations and reflections of itself). We observe that the placement of the eight queens (if labeled with eight distinct symbols) represent a transversal of the chessboard.

How many solutions are there for the 8 queens problem?

The eight queens puzzle has 92 distinct solutions. If solutions that differ only by the symmetry operations of rotation and reflection of the board are counted as one, the puzzle has 12 solutions.


1 Answers

One possible solution:

def collision(x1, y1, x2, y2):
    return x1 == x2 or y1 == y2 or abs(x1-x2) == abs(y1-y2)

i.e. there is a collision if the two points are on the same horizontal row, same vertical row or same diagonal (vertical distance == horizontal distance).

like image 140
dF. Avatar answered Sep 18 '22 16:09

dF.