 What have you two Mike the Coda here today? We're gonna do chessboard and queens your task is to play eight queens on a chessboard So then no two queens are attacking each other Additional challenge is that you can only place queens on free squares and reserve squares Each square is either free or reserved and you can only place on Free squares. However, the reserve squares do not prevent Queens from attacking each other Okay, so how many possible ways are there the place of Queens? So that's the problem The problem is asking how many possible ways to place the Queens and how do you do this? Well, you're given all these given this eight by eight eight by eight Right these eight by eight grid and we have freeze and reserves so Reserves don't Don't stop. I do not prevent Queens from attacking each other. Okay, so you only can play on you're gonna replace on a free Free spot, which is a dot so we're given this eight by eight board and we don't we want to place Queens So that no two Queens are attacking each other and we can only place on a dot or a and we cannot place it on a star Okay So how do you do this problem? All right, so first of all, let's see what a queen can do. So if I place a queen on this spot Queens can only go Queens attack diagonally and Same columns same row and diagonally. So if I place a queen here it's gonna attack these Diagonally downwards and it's gonna attack the row and the column. So this is the column and then use a row Okay, so if I place a queen here, I Cannot place a queen anywhere else along these lines. Okay, because then is that's how a queen attack stuff? Okay, also if I place Another thing is like let's say place a queen here So remember the rows you cannot place on the rows and you cannot place on the columns right because That's how queen to attack but also the diagonal. So Diagonally going down. You cannot place it on here, right? Because it's a diagonal going down right He also cannot place it on here because it's the diagonal from bottom to top So yeah, these are the places you cannot place a queen that Okay, so if I place a queen here, I cannot place Another queen in any of these spots on the lines. Okay So, how do you do this problem? simple way is to create a Create a boolean array representing The row representing the columns The columns that are the queen is placed on and also the the diagonals that it's placed on So remember if I place a queen here I cannot place a queen on the diagonals so if we create a boolean array that represents diagonals and also the columns and Let's say we recursively place it on there each of the actually yeah, we let's say we recursively place in each row So let's go through and just place a queen on each row But what we're gonna do is we have to check if it's not on the same diagonal and not on the same column Okay, and remember we need to place Eight of these queens eight of these queens. Okay, so that no two queens are attacking each other Okay, so let's create a boolean array representing the diagonal going from top to bottom and bottom to up and also the columns So, how do you create a boolean array representing the diagonals from top to bottom? Actually, but let's do bottoms top so bottoms top. Well, it's pretty easy. So let's say I have these rows One two zero these indexes of these rows Right and then these indexes for the columns right So if I want to place Let's say I want to place something Let's say I want to check the diagonals from Let's say zero zero from bottom to top. Okay, so these diagonals, right? These diagonals this diagonal this diagonal This diagonal Okay, so apparently if you were to add up the rows and column All the diagonals would have the same number. So This row plus this column zero plus zero is going to give us zero And then one plus zero is going to give us one also a zero plus one will give us one two plus zero is two two plus one no one plus one is two Zero plus two is two. So you guys see the pattern, right? This would be three I'm just adding the row in the column. They're gonna have all the same diagonals Right, you guys see the duck you see the see the pattern. So if I continue doing this so on and so forth for all the diagonals I could just have a boolean array Representing where the index represents the The diagonals that are already set so here if I create like a For the diagonal from bottom to top if I create a boolean array representing all the row plus the column so the row Row plus column. I'll do our plus column row plus the column Right and the indexes represents these zero one two three four five six seven Six seven yada yada yada, right? So then if let's say I place a queen here at one zero at this spot here, right Then since one plus zero is one, right? I could just set this to be true and then because of this is true If I place try to place a queen at the at another diagonal, that's at diagonal one I just have to look up at the table and see hey, this is already true So I cannot place a queen there. Okay? You can also say to do the same thing. So let's say I place a queen at diagonal four So like any one of these four diagonals, right any of these values that have a diagonal of four So let's say I place a queen here Right Then since two plus two is four, I could just put true here on the four my boolean array and then that means that no matter what the No matter what I cannot place any queen I just have to look up Number four and that means that I cannot place any queen on this diagonal. Okay So yeah, this is from diagonal from bottom to top and the nose do the same thing for top to bottom So to do top to bottom the math equation is pretty much the similar but It's row minus column plus seven and the reason why we plus seven is because the last number is seven Okay, so Here from top to bottom This one's bottom top right bottom top now top to bottom. So let's see zero plus six Zero minus six is negative six plus seven is one. So here's one one I'm zero minus seven is negative seven plus seven is zero. So this is zero. So then The numbers would look like this Three three three three three and so on and so forth and you could see here that the diagonal here The diagonals from top to bottom would be like this right These are the diagonals Okay, so this is the top to bottom when I say top to bottom. I mean like like this way Right this way Right all these diagonals So if we do row row minus column plus seven We realize that from top to bottom They would have these the same numbers and then we use the same same situation using a Boolean array Representing if we set it at the certain diagonal already and then we cannot place it again at that location Right at the same row plus column a row minus column plus seven Okay All right. So once we have this It's all we have to do Now as a matter of time of you also need another Boolean array representing the columns you placed at Yeah, yeah columns you placed at Yeah, I think it's going to place that the reason why is because the Queens attack column by column Queens also attack columns, right? So we need another Boolean array Representing the columns that we already placed at and we can just look up at this current index to see if we already placed it there Okay, and um, yeah, and we could just recursively like add the Yeah, because recursively add like values for like the rows or the columns depends on how you're doing it I like to place it by actually I'll create a Boolean array for the rows. Okay, the rows because um Yeah It really depends on how you want to do it You just have to look up on the certain rows or columns you've already placed it at So you need another Boolean right for one of those and then lastly you just recursively call and use backtracking So, yeah, I'm gonna show you guys the code now. So here so here we have the character 2d character of characters and Then which is our and then I'm just reading in the array Are here read in see an R at ij. Okay, and I'm gonna call this function called solved And I pass in the current index of zero and it's gonna recursively call and place Queens on these boards until We cannot place any Queens left and I'm gonna create this answer It just keeps tracks the count of all the Queens that I can place without the two attacking each other and Then yeah, I just print out answer Okay So how does solve the work? So solved works by Yeah, okay, so Let me just look at rookie. Okay, so solved. I pass in zero as a current column. Okay, so I'm what I'm doing is I'm just placing Placing each piece at a certain column. Okay, and And yeah, and if the current column is equal to eight I'm gonna add one to the answer in our return and the reason why is because if there are eight pieces on the board Remember, we have to place eight pieces on the board And if we successfully place all eight pieces on the board and the no two Queens are attacking each other Then that counts as one count, right? That means that that's one way you could place the Queens, okay? So once we place all eight pieces Queens on the board We increment the counts of Queens we have By one and then we return. Okay Now here's a really tricky part. All right So yeah, here are the Boolean arrays I'm gonna loop through each row. All right, and the rows are there eight rows So what I'm doing is I'm going through here. I'm going through each of these rows. So each of these rows I'm gonna go through each of these rows. Okay Then what I'm gonna do is I'm gonna check of My current board if it's a dot and the reason why at the current row in the current column is That we can only place it if there's a dot at the current row in the current column, right? Back to our problem statement. We cannot place Queens at Stars, right? We can only place it on free squares of dots. So that's why I have an if statement here to make sure that at the board It's a dot right at the board. It's a dot. I only can place it when there's a dot. Okay now I Create a boot these three Boolean arrays representing the left diagonal The right diagonal and then the placed columns that each of these Boolean arrays represents if I already placed Queen at the left diagonal the right diagonal and Current column. Okay, so since it's Boolean that means if it's false that means Nothing's there. It's right Since it's Boolean that means it starts out with false So if I didn't play if it's equal to false that means I didn't place a queen there yet, so it's free Okay, so I've left diagonal is equal to false at row minus column plus seven. So that's this Diagonal going down, right this way Is equal to false and The right diagonal is also false. So that's row plus column, which is this way this way, right? and The current column at place columns at the row is Equal to false. Um, actually this should actually be called placed rows I'm gonna change this real quick. This should be placed rows placed rows. I wrote it wrong Yeah, it's placed rows. So if if current if I didn't place it up this current row current row is equal to false right, so if Place the rows represents all the rows that I already set It's another Boolean array that represents all the rows. So did I place it here? Did I place it here? Did I place it here here here here? Okay, so if the current oh My gosh, if the current place rows is equal to false at the row is equal to false And that means that it's free, right if all these are free if they're all these are equal to false and this and The board at the dot at the row and column is a dot that means I could place it So they're all free since they're all free. That means I could place it. Okay. Here's the part where the backtracking is a pretty difficult So yeah, I also have this Boolean array representing the locations of that. It's already placed So yeah, this Boolean array is an 8 by 8 array and it's 64. So a total of 64 squares It's just the same thing as this as a board But every time I place something at the row and column I set it to true So yeah, I'm gonna set the place at the row and column row and the current column to equal to true and Then I'm gonna set all the left diagonal red diagonal and the place roads to be true because that means that hey We already placed it there. Right. We already placed it at the left diagonal So we're so let's say I place it here, right? Then that means I have to set all these values to be true and I cannot place any queen at it again I need to check if the place row is already true. So all these values I'm gonna set these values to be true again because we cannot place them on the row and then Right diagonal. So I have to place all these values to be true, right? So you cannot place it at the Right diagonal. So then these values are all true. So I set them to equal that, okay? So this is just saying that to equal the true so you can't place it again at those locations Then I'm gonna recursively call on the next column. So After I place that this column of this two I'm gonna do the same thing on three and then on four and then on five and what that's gonna do is that It's just going to make sure like these values are gonna be player blah, blah, so on and so forth. That's what's gonna do. So, yeah Okay Yeah, so our curse we call current column plus one so I go to the next column to place it Then what I'm gonna do is do is I'm gonna set placed row and column to equal the false and then I said all the other values to be false again and the reason why I do this is because If there are false then that means that Yeah, if if they're false Essentially is that wait hold up. I actually never used placed. Did I use place? I think I ever used place Whatever, it's fine. Yeah Essentially is that the reason why we we set to false now because there's two scenarios, right? If I place a queen there Right if I place a queen at a row and column That means that there's a bunch of other arrangements when I place at that location, right? But there's also a chance that I don't place it there and we have to calculate those Scenarios also, so that's why we have to set equal to false afterwards because there's other scenarios There's two once you place it there. There's other scenarios when we don't place it there So that this will keep keep track of that I'm gonna see if I ever used placed This to to the thing. I don't think I ever did Yeah, I don't think I ever did technically we could just get rid of this Okay, um Yeah, we could get rid of this Because we already have place rows And we never ever actually check it in the if condition, right? We have the place for over You have left diagonal. We have right diagonal and then we also have the check in the board So we don't actually need placed. I think yeah, I think it still works So, yeah, um, let's go Submit it again. See if just in case. Yeah, that's pretty much the gist of the code Hope you guys enjoy this video Yeah, just of the code is just to recursively Go to each column and then we have Boolean arrays to represent like the diagonal left and the right and then also the The rows replacing and then yeah that these these Boolean arrays just check if the like you already placed it at that location So then you could set it to true and then recursively call By calling in the next column and then set equal to false Yeah, I still got you seed. Yeah, so I never used placed actually That's crazy. Okay. So yeah, um, I hope you guys enjoy this video Ray com subscribe These lengths are 30 just because I just I just needed to be an arbitrary length It could be anything as long as it's between like was it 7 plus 7 14 as long as between 14. It's fine But yeah Yeah, so yeah, I'll just re-explain again really quick the We pass on our current column when our when all the Each of the columns are equal to eight right when we place eight queens already we add one to the answer and return otherwise, we're gonna go each row make sure that the Row and the current column is a dot so that we could place a queen and then we check the Diagonals make sure they're all false and also that we didn't place that at the current row Okay, then if it's if they are all false We're just gonna set those left diagonals right diagonals in the place row be true and then This ensures that we cannot place place it at the diagonals again, right at the diagonals and the rows And I recursively call on the next column and then what I'm gonna do is I'm gonna set all the diagonals and the Placed rows to be false because there's two scenarios where I either place it at if I either place it at the queen At that location or I didn't and then this is gonna account for all the other ones At the after the end we just print out the answer Our of our count and then yeah, that's it. Hope you guys are this. I hope you guys enjoyed this video Right come subscribe. I'll check guys later. Peace