Welcome back, everyone.
So we just figured out how we're going to store the values that we've put inside of each Griselle according
to what's in the row, what's in the column and what's in the individual, three by three squares.
Now we want to cut out the actual solution.
So to start, you'll see that I just reorganize things a little bit.
I've moved our get box ID function down here.
I still have that template that we were looking at.
And over here I've initialized our actual function where we receive the board.
So to begin, we need to actually now initialize the data structures that are going to store the values
that we need to begin.
I'm just going to initialize something called rows, columns and boxes.
And this is going to contain the arrays that will end up holding the hash maps that tell us how many
values have been added to each respective data structure of that type.
So here I'm just going to say first, let's get the end value, which is going to be our board size.
So it's going to be nine, which we know for sure.
But just in case, we'll just initialize it separately.
Next, what we want to do is initialize our three different variables, so, one, I want boxes to be
equal to a new array of size and.
Then I also want Rose to be the same thing, so a new array.
Of Size N and columns is also a new array of size and.
Now we need to fill them with any different hash maps, we could have done the dot fill map method we've
done before, but because we need to do it for all three, it's easier in my mind just to do a follow
up.
So I'm just going do for let I equals zero I less than an hour plus plus I'm just going to go boxes.
I equals a new empty object rose at I, a new empty object, and then columns I.
A new empty object, so now that we have the data structures that will help us keep track of whether
or not we're violating any of our constraints, what we need to do is fill them with the initial values
inside of the array.
So what we want to do is we want to scan through this entire 2D array, so entire board.
And if we see a value, these are values that come by default with the board.
So we cannot change them.
We need to add them automatically to either our boxes are rows or columns, depending on where they
actually fit.
So this is where we're just going to loop through the entire board.
So I'm going to say four letters are equal, zero are less than an.
Ah, plus, plus.
Then I'm going to say again for let's see, equals zero C less than an C plus plus.
So at this point, what we need to check is whether or not the board presence there has a value, the
easiest way to do this is to ask what the empty cell is going to contain.
Is it just going to be undefined or is there some kind of stipulation here?
Generally speaking, there's a couple of ways you can do this.
You can assume that if, for example, in the grid cell, if it's empty, then the value that's there
is not going to be greater than zero because we can only hold the values one to nine, which means that
if it's empty, it's going to be undefined or they're going to use zero or they're going to use any
kind of character to hold an empty space.
In this particular case, with this question, it's going to be a period which is a string.
So we're just going to say that if board.
At ah and at see, so at the current GRISELLE does not equal period, meaning that it already contains
a value.
Then we're going to add and fill it into our respective board rows and columns at the correspondingly
correct placement and inside of their hash map.
So first of all, we need to do is we need to get the value.
So here we're just going to initialize it as Val is equal to board at our.
Then what we want to do is we want to get the box ID, which we already can easily do using our get
box ID function that we wrote in our last lesson.
So here I'm going to say const box ID.
Is equal to get Bogside.
And we just passing the R and the C value, and we'll get back the correct backside where this current
GRISELLE lives, and then it's very simple.
We just do boxes.
Bogside.
At this value is equal to true because we're using a Hashmat.
I do want to mention that you could use a set if you wanted to, which is a special data structure that
holds unique values, but I'm just going to continue using a hash map because that's what we've learned.
We're going to do the same thing, Rose, at our value is now also equal to true.
And then columns at sea at this value is also equal to true.
And that's it that will set us up for all of those initialise values that we have inside of the board
so that we now know what to do, if they are duplicated, then all we need to do is write out the actual
recursive function that will drive the check and the filling of the pieces into the board and also the
backtracking.
This is all going to happen through one function so we can call it solved.
Backtrack.
And we just need to think about what's going to be added to this function as a capital B..
So here, we're going to pass in the board.
Of course, we're going to pass in all of our data structures that are storing what values get repeated
so boxes, rows and columns.
And we're also going to pass in the values that we're going to start from for our R and R, C, respectively.
So, of course, we're going to start from the top left corner.
So we're just going to initialize it at zero zero.
So when we cut out this function, it is going to be pretty large and there's a lot of considerations
we have to make as far as the constraints of the box that we're coding in itself, not necessarily just
the actual constraints of the logic of whether or not a value in the Sudoku board is truly valid.
We do have to consider that as well.
But there are a bunch of these little edge cases that will occur that you want to think about.
So before we move on to the next video where we'll code this out together, I do want you to try and
think about what these different constraints will be.
And then once you're ready, move on to the next video.
I will cut out the last portion, which is our solve backtrack function.