0:00
Hello.
00:01
For problem 18, you're asked to solve the eight queens problem.
00:10
So you want to place queens on a chess board so that no two queens are able to attack each other, which means there are no two queens in the same row, no two queens in the same column, and no two queens that are diagonal to each other.
00:28
And so there are lots of ways to go about solving this puzzle.
00:35
I'm just going to present the way that i chose to do it, which is one way of doing it, but know that there are multiple options as to how to do this.
00:45
So we're going to start by setting up our board.
00:48
I'm making a board that is 8 by 8.
00:53
Well, i guess a row is 8.
00:55
And so our board will be 8 by 8, and it's filled with zeros to start.
01:01
So i'm going to work with numbers to start, and then at the end, i'm going to, when i print it out, make sure that it displays it in a way that is similar to the display in the textbook.
01:14
So we want to define a function that checks whether or not there is a queen within attacking distance.
01:23
So define attack from position ij in the board.
01:29
First, we need to check vertically and horizontally to make sure that there are no queens in the same row or columns.
01:36
So for k in range 08, if board ik is equal to 1 or board k, j is equal to 1 return true, because that would mean there is a queen in a row or column, and so you would be able to attack that queen.
01:57
We also need to check diagonally.
01:59
So we'll look for k in range 0 to 8 and l in range 0 to 8, and if k plus l is equal to i plus j or k minus l is equal to i minus j, and if kl is equal to one we want to return true so again we want to look at the diagonals and so if there is a one in one of those diagonal positions then we need to return true otherwise we want to return false so that is our attack function now we want to define queens and this will be the function used to place queens on the board and i'm making this a function of nward n is the number of queens.
02:48
So if n is equal to zero, you're not adding any queens to the board.
02:53
We're just going to return true.
02:56
For i in range 0 to 8 and j in range 0 to 8, we want to place a queen on the board if possible.
03:04
So if attack ij is not true, so it's false...