Heres one neat way to express the N-queen problem. Simple math shows that if two queens can attack each other, one of the following must apply:

  • They must be on the same column.
  • They must be on the same row.
  • The sum of the row and column must be the same for both( \ diagonal).
  • The difference of the row and column must be the same for both ( / diagonal).

 

It can be thought of as a simultaneous equation with 2N unknowns.

Find integers x1 ... xn and y1 ... yn in the range 1 .. n such that :

Given integers A and B in the range 1..n and A ≠ B

  • xA ≠ xB ( No two queens on same column )
  • yA ≠ yB ( No two queens on same row )

 

For all combinations of integers A and B in the range 1..n and A ≠ B

  • (xA + yA) ≠ (xB + yB) (No two queens on \ diagonal)
  • (xA - yA) ≠ (xB - yB) (No two queens on / diagonal)