i think your problem is only these 2 lines:
Y-Y1=/=Xdist,
Y1-Y=/=Xdist,
it checks whether the queen which has Y, attacks the queen in the row with a distance of Xdist in a diagonal way or not. (|Y - Y1| = Xdist --> diagonal attack
).
the other way to attack other queens is to qttack them in the same row, that doesn't happen simply because the solution is a permutation of [1,2,3,4,5,6,7,8]
. so something like [1,1,3,4,5,6,7,8]
never happens and thats enough to check diagonals.
I hope it solved the problem.
p.s. note that Y1
is the Y coordinate of the head of the Others
from the rule Safe/1
. so it is a queen with a Xdist
of 1 at first, then it backtracks to other rows.
clarification
we are talking about noattack/3
. you give it three arguments:
first: Y
coordinate of current queen,
second: a right-most list [Y1| Ylist]
the begins somewhere after where Y
is in the list, and continues to the end of the primary list.(yes this is a sublist of the solution), and
third: Xdist
is the index-distance between current queen (which has Y) and the queen which is gonna be checked with the current queen (which has Y1 and is the head of second argument).
third argument is necessary because without it we can not check diagonal interaction between the current queen and and the queen which has Y1. it is really basic mathematics, you have 2 points in the coordinate system with only natural numbers. lets say these 2 points attack each other in a diagonal way if and only-if abs(x1 - x2) = abs(y1 - y2).
Example #1 . and if you understood my explanations well, check it as Accepted.
P1 = (3, 4) and P2 = (1, 2) -->
these points have a diagonal attack because abs(3-1) = abs(4-2) = 2
Example #2
P1 = (3, 4) and P2 = (7, 0) -->
these points have a diagonal attack because abs(3-7) = abs(4-0) = 4
this is why we check both Y1-Y =\= Xdist
and Y-Y1 =\= Xdist
. because whenever there is a diagonal attack at least one of them is gonna be true
. when non of them was true, it means that there is no diagonal attack between the queen with Y
and the queen with Y1
. so we can proceed to checking the next queen which is the head of Ylist
.
I hope that's enough. this is a very easy problem, Just read it carefully and if you couldn't understand again try to trace the algorithm on a piece of paper yourself. that's a way always works.