Now, when you know what backtracking is,

you will have a chance of using this powerful method to solve

another challenging puzzle which is called 16 Diagonals.

The goal in this puzzle is to place many diagonals into

a square grid such that no two diagonals have a common point,

or in other words, they do not touch each other.

So we start with a very simple version of this puzzle.

In this case we are given a three by three grid and our goal is to

place six diagonals in this grid.

It is not very difficult for this case,

so you see one solution here on this slide.

And this is another solution, where again,

we have six diagonals, and no two have any common point.

Now let's gradually increase the size.

Let's consider a four by four board.

Now, the question is to place ten diagonals.

It is already not so simple,

but here you are.

This is a situation where there are 10 diagonals.

And now we come to a challenge.

In this case we are given a five by five grid,

and our goal is to place 16 diagonals.

You may want to first,

place such 16 diagonals by hand and you will find out that it is not so easy.

So instead of trying to do this by hand you may

also want to implement a backtracking procedure.

This is your exercise, actually.

And this is a suggestion for you.

So, to implement that backtracking program for solving this puzzle,

you may want to do the following: So consider your five by five grid.

So this is 25 cells,

which are initially empty, or,

to be more formal,

let's assume that initially we have minus one in all these cells.

Minus one indicates that we haven't yet decided what to put into all these cells.

And then we start processing all these cells one by one.

Say you start from the first row and

you first consider all the first row and the second row,

and the third one and so on.

So for each cell,

for the current cell you do the following: You consider all possibilities for this cell.

Either it has a diagonal which goes from this corner to this corner,

or you consider a diagonal which goes from this corner to this corner,

or you consider a case where there is no diagonal in this case.

So for example, you may store just an array of 25 cells where,

in each cell you store either minus one,

which means that you haven't yet decided what to put into this cell,

or, for example zero,

which corresponds to the fact that there is no diagonal in this case,

or one if there is

a diagonal that went from this corner to this corner,

or two if there is a diagonal going from this corner to this corner.

Finally, when you place a new diagonal,

when you fill in a new cell,

if you place a new diagonal into this cell,

you need to check whether it produces

any conflict, whether it touches any other diagonal.

And this should be implemented very carefully.

And if it doesn't touch,

then you continue trying to extend it.

If it touches, you stop immediately and you backtrack.

OK? Well good luck implementing this program.