- Despite success with the N-Queens code,
I wanted to try it again.
Specifically, I wanted to work on the recursive routine
to ensure that I fully understood the diagonal attack.
Because my stumbling block was the single-dimension array,
I figured I would return back to
a two-dimensional character array.
Unlike my first disastrous attempt to solve this problem,
I would track only the queen's location,
and not her attack vectors.
So, this third solution was really a combination
of both my previous attempts.
With the emphasis on making a diagonal attack
easier for me to code.
Two-dimensional character array is faked right here,
at line 62.
I use a pointer instead of two-dimensional array notation
simply because this is the C-language,
and pointers are a big deal.
Of course, that also requires some pointer math,
but for some reason,
my brain could handle that type of math
better than the row-column thing
going on with the single-dimension integer array,
as covered in the previous movie.
Space is allocated for the array.
Then it's filled with empty spaces.
Unlike my first attempt, I used only spaces and Qs.
And I didn't really need to use the x and y for loops
to fill the array.
I wrote this code simply to enforce in my brain
that the array is dealing with rows and columns.
The find square routine in this function
is very much the same
as the function in my N-Queens two dot C source code file,
covered in an earlier movie.
The difference here is that a two-dimensional array
is being faked, which I found easier to deal with.
Columns and rows are scanned.
Vertical attacks are detected
by checking the current column for a Q.
If found, the next column is scanned.
The board buffer must be increased by squares times row,
or this row to get to the current row offset.
Then column is added.
That's just the math necessary to translate a buffer chunk
into a two-dimensional array.
In fact, the compiler does this math internally
any time you use 2D array notation.
Vertical distance is easy.
It's the current row, minus this row.
To find a queen in the upper left diagonally,
I confirm that a valid position is available
greater than or equal to zero.
And then I check that location for a queen.
Likewise, in the upper right,
I confirm that the position is valid,
and then check the square for a queen.
For some reason, this solution worked better in my head.
The code actually ran the first time,
doing exactly what I'd plotted out.
This contrasts with the integer array approach
which I couldn't get right without experimentation.
Effectively, they are both the same technique.
Just applied to different types of array.
Still, I'm far more pleased with this solution.
One more thing.
Because I'm placing a Q into a position in an array,
once that position has been tried,
the Q must be removed.
That happens right here.
This statement stems from the lesson I learned
in my first attempt.
But all we need to do is just erase the Q
and not the asterisks.
So, unlike a single-dimension integer array,
with a two-dimensional chessboard array,
I must manually remove any extent queens from current row
before the code continues to scan the rest of the columns
in that same row.