http://jeffe.cs.illinois.edu/teaching/algorithms/notes/03-backtracking.pdf
explain the highlighted texted of image
Q[i]=j Q[i]=j+r-i Q[i]=j-r+i
how these statements check if two queen attack in row column or diagonals?
http://jeffe.cs.illinois.edu/teaching/algorithms/notes/03-backtracking.pdf
explain the highlighted texted of image
Q[i]=j Q[i]=j+r-i Q[i]=j-r+i
how these statements check if two queen attack in row column or diagonals?
That's a better solution than I came up with many years ago when I beat the obvious algorithm by an order of magnitude.
Fundamentally, what this is doing is instead of maintaining a board and checking the three paths that a queen can move (you only need to look behind the queen--it's the only queen on the row so you know that direction is clear an the queens ahead haven't been placed yet) which requires checking (row - 1) * 3 cells (although some are off the board, it may be cheaper to test the diagonals for bounds first) it's maintaining a one dimensional array of queens and noting what they threaten. This reduces each placement test to 3 squares. (And uses only addition, no multiplication. Irrelevant now, a considerable time savings when I was playing with this.)
I haven't looked over the whole thing to see how they are doing it with one array rather than the three I did it with (column threatened, / diagonal threatened, \ diagonal threatened. The latter arrays are twice as big.)