- 35 minutes ago
Transcript
00:00Next question which we will talk about is one of the most famous problems when we talk about recursion, backtracking,
00:05in fact coding
00:06It's called N-Queens, or N-Rania
00:09We have seen a chess board in this question, we have given a chess board
00:14Chess doesn't come from a pro, there is no problem
00:20There are basic rules in chess that there is a player, usually the board has some pieces
00:26and there are some defined steps in each piece
00:28So here we will discuss the defined steps of the queen
00:32I have a chess board in question, and I have N-Queens also given N-Queens
00:41Hence the name of this problem, which is N-Queens
00:43In this N-Queens chess board, I have to keep N-Queens in this way, that no one can attack
00:49the two queens
00:52Because the two queens do attack the other one in which position?
00:54Let's see it first, let's see it
00:56Let's assume that I have a chess board
00:59It has some size
01:01Suppose this is a 3x3 chess board
01:04In 3x3, I will have 3 columns and I will have 3 rows
01:09Where can the queen attack?
01:11If I have put the queen here, then my queen can attack vertically
01:20You can attack vertically up and vertically up and diagonally up and diagonally down
01:27In all directions, I can attack my queen in these directions
01:32You can attack the other two queens
01:33You can attack the other two queens
01:33You can say, N-D-D-D-D, the chess board is completely filled
01:37But it doesn't need to be bigger than my chess board
01:40If I have a chess board, then it will completely fill
02:01Now, if I have a queen in a random position, I will put it here
02:05Then what will happen?
02:06The queen will make the other queens unsafe
02:10Because if there are other queens here, they can attack the other queens
02:16If there is no one sitting, then they can attack it
02:18So, it has made it unsafe, it has made it unsafe
02:22It has made it unsafe, it has made it unsafe
02:23Diagonally up here
02:25Who has made it unsafe?
02:27Left is made it unsafe
02:30Here is made it unsafe
02:32Here is made it unsafe
02:33But if I put my other queen here
02:35Then it is completely safe
02:37She is not in these zones
02:40So, this queen is completely safe
02:42With this, where can I sit my other queen?
02:44My other queen can sit here
02:46This is not in this queen
02:48This is not in this queen
02:50Where is the queen?
02:53This will be in this one
02:54This one, this one, this one
02:57This one, this one, this one
02:57This one, this one, this one, this one
02:57So, from this way, there are many random positions possible
03:01But in the coding, I don't confuse it
03:04What positions will be made
03:05I have to think mathematically about things
03:07I have to think logically and rationally
03:10That I have to think of what permutations
03:12What combinations can be made
03:13For example here
03:14N is equal to 4
03:16I have a board of n cross n 4 cross 4
03:19That board looks like this
03:21I have 0, 1, 2, 3 columns
03:24I have 0, 1, 2, 3 columns
03:26I have rows
03:26This is a safe combination
03:28Here is a queen
03:30Here is a queen
03:43In the first row
03:45In the second row
03:46In the third row
03:47In the fourth row
03:49In the fourth row
03:50In the fourth row
03:51This is one of the possible solutions
03:53For n is equal to 4
03:55Now, when we analyze the problem
03:57N queens
03:58In the third row
03:59In the third row
04:00You have to calculate all the solutions
04:02You have to calculate all the solutions
04:04You have to take out all the solutions
04:05And all the possible chess boards
04:07How can we print the chess board?
04:10How can we print the chess board?
04:11We can take the chess board
04:13In the 2D array
04:14If there is a queen
04:16Then why the character will be stored?
04:18If there is not
04:18Then we can store x
04:20Or not
04:20Then we can store dot
04:21Or leave the blank
04:23Or leave the blank
04:23But where there is queen
04:25Why will come here?
04:26In this way
04:27We can visualize the chess board
04:29In code
04:29There will be a problem
04:31That you have to give
04:32All the solutions
04:33Or
04:33If you find
04:34There is a possible solution
04:36Is there or not
04:37Yes or No
04:37If you have
04:38Then you print one solution
04:40You can print one
04:40It will be
04:41Count
04:42Count solutions
04:44I mean how many possible solutions will be
04:47How many possible ways will be
04:50We can set up n queens on chess board
04:54Such that no two queens attack each other
04:57These are three possible questions
05:00But in questions we need to find out the same thing
05:02You can ask all the solutions in the interview
05:05Count how many solutions are
05:08How many permutations are
05:09So what do we do? We will do the most difficult
05:11And then we will derive both
05:14We will remove all the solutions
05:15And then I will show you how we can write these two codes
05:20So what do we do?
05:21We will remove all possible solutions
05:25Which we can set up n queens on a chess board
05:29Now I will say a little logical thing
05:32I have seen queens move
05:35Queen's move means vertical
05:37Horizontal
05:39Horizontal
05:40And diagonally
05:41Now if I see horizontal move
05:45If my queen is here
05:47And this is my chess board
05:49Single row
05:51So what can I say
05:52Single row
05:53No one will be here
05:55No one will come here
05:55No one will come here
05:56No one will come here
05:57No one will come here
05:58Only where will come from the row
05:59The row will come from the lower
06:01And the other queen
06:02Because when a queen is sitting there
06:04It will lock the whole column
06:06And the whole row
06:08So I will take the row
06:10And the same thing
06:11As my queen is sitting in a row
06:12So if I can set up n queens
06:13Then it will lock it completely
06:15And in the same row
06:16I will not set up the same row
06:20So if I have n is equal to 2
06:23So if I set up n queen
06:26Then I will set up my queen
06:30Then I can say
06:42such that no two queens attack each other.
06:44The first problem was n by n, chess board.
06:47Now I have to simplify it and say that
06:50n queens have to be placed in n rows.
06:521 queen to 1 row, 1 queen to 1 row, 1 queen to 1 row, 1 queen to 1 row.
06:58If I can remove the possible solution,
07:03we will forget that queens can attack each other.
07:07First of all, let's focus on how can I be placed on queens.
07:10How can I put n queens in n rows?
07:14Whether they are attacking or not.
07:16For example, I will take a small example.
07:19n is equal to 2.
07:21If I put queens here, what will I do?
07:24I will talk about row number 1.
07:27If i is equal to 0, if I put queens here,
07:31I will put queens here.
07:31Now I can't put queens here.
07:35Where will I put queen?
07:36Where will I put queens?
07:37The next queen will put i is equal to 1 row.
07:40I will put queens here.
07:42I will put queens here.
07:43I will put queens here.
07:44Now I can't put queens here.
07:46My first possible solution is to put n queens in n row.
07:50If I put n value to 2.
07:53My first possible solution is to put queens here.
07:56I will put queens here.
07:57I will put queens here.
07:57Why do I put queens here?
07:59and my second queen will come here.
08:01After that, what will I do?
08:02After I will put them here,
08:05I will put queens here.
08:07Where will I put queens here?
08:08And I will put queens here.
08:09And that will be left for me.
08:11So I have another possible combination. Queen's.
08:15I didn't see the attacking thing. I didn't attack or not.
08:19Now I just want to see how I can put Queen's.
08:22N Queen's. N Rose's.
08:26Where's the queen? Where's the queen? Where's the queen?
08:28So I've put the queen here and here.
08:32What do I do now?
08:34I don't want to save Queen's. I'm going to go up and see.
08:37I mean I'm back here. This is basically backtracking.
08:42I've put the queen here.
08:43Now I've put all the combinations here.
08:47So now I'm going to backtrack.
08:48Backtrack and I've put it in this position.
08:51Now I'll put it in the second position.
08:52So my queen will come here.
08:54So now I have to put it in the lower row.
08:56I mean i is equal to 1.
08:58I've put it in the first position here.
09:01So what will my third permutation become?
09:04I've put it here.
09:06And in the second row my queen is here.
09:08Now I've put it in the second row.
09:10Now I've put it in the second row.
09:11Now I'm going to put the queen here.
09:13So I have one more possible solution.
09:15What is the possible solution?
09:17So once my queen is here.
09:19And the second time she is here.
09:20So I want to put it in the second row.
09:22If I want to put it in the second row.
09:23Whatever I can put it in place.
09:36I don't have to check attacking now
09:37Is it attacking or not?
09:39He is obviously doing it
09:40He is attacking and he is attacking
09:41He is attacking and he is attacking
09:45It doesn't mean there is no right solution
09:47But how can Queen play play?
09:49I have analyzed it
09:51How do I analyze it?
09:52I will go row wise
09:53I will start in 1st position
09:54I will start in 1st position
09:56I will start in 1st position
09:58I will start in 1st position
10:05I will start in 2nd position
10:06I will start in 2nd position
10:08I will start in 2nd position
10:11My process will remain
10:13And these are 4 final solutions
10:16Now we will write N queens
10:19But they will write N queens
10:21In N rows
10:23We will not check the total conditions
10:25What will I do in the code?
10:27I have a N is equal to 2
10:28First I will make chess board
10:31Or I will say
10:32My character would
10:35What will its size?
10:36It will be new care of N by N
10:39My board is N cross N
10:40And I will initialize it
10:43How will I do?
10:44This will be my initialize step
10:47In the beginning
10:48I have no queen
10:49So I less than N
10:52I++
10:53For int J equals 0
10:56J less than N
10:57J++
10:58In the beginning
10:59In every position of my board
11:01What do I do in the board of I, J
11:02What do I do in the board of I, J
11:04I will just put a dot
11:05I mean, this place is empty
11:07There is no queen
11:08There is no queen
11:08So I have initialized
11:09In this way
11:09I have initialized
11:12One time
11:12I have initialized my chess board
11:14Now I will call
11:16I will call
11:17N queens
11:17N queens
11:18N queens
11:19Call
11:21Passed into this
11:22I have my board
11:22And where I started to put it
11:24Which row
11:25I have also passed that row
11:28Which is
11:280
11:30Now I will make my function
11:32Public
11:33Static
11:34Void
11:35N queens
11:37N queens
11:38Under my character board
11:39I have a parameter
11:41Another thing
11:41What will I do?
11:42The other thing is
11:42My row number
11:44Which row
11:45I am in this
11:45I will go into one column
11:47And there
11:48I will start to put my queen
11:49So what will I do?
11:51What will I do?
11:51I will put a loop
11:52J is equal to 0
11:54J less than board
11:56Length
11:57J++
11:58What will I do?
12:00I will not see that
12:01Queen will be safe
12:02Or not
12:03I will just put the queen
12:05To put it
12:06To put it
12:07I will put it
12:08I will try to do
12:10Which row
12:12I will try to put
12:13I will put it
12:14I will put it
12:15I will put it
12:16Which is
12:16My row
12:16This is my row
12:18I will put it in one column
12:20And then
12:22N queens
12:22I will put it
12:23Which row
12:24So I have put it
12:27I will put the row
12:28And what did
12:29I have put it
12:29The row
12:30And then
12:30I will put it
12:31The two row
12:32Next day
12:33The third row
12:36I will put the next 4th queen in the 4th round
12:38Now, as I will backtrack the calls
12:41They will come back here
12:43The queen went from here
12:44Then the third, the fourth, the fifth
12:46Then when the backtrack will come back
12:49Then I will come back to where else
12:50I will take the queen from there
12:52Because there will be a queen in one row
12:55If the column is in the J
12:57Then I will take the J plus 2
13:00Then it will come back here
13:01And J plus 2 will come back here
13:03Then I will take the J to backtracking
13:07So what will I do?
13:09I will create a board of row J
13:11I will create a dot again
13:15This Q means here I have put the queen
13:17Then I have put the N queens in the next row
13:20Then I have put the N queens in the next row
13:23Then I will take the backtrack again
13:23Then I will take the queen again
13:25And now the place is left
13:27So I will put the N queens in the N rows
13:31And where are I going?
13:32I have put a loop in the column
13:35Now if I want to see my base case
13:37What will happen here?
13:39If my base case is equal to any point
13:40If my row is equal to my board
13:44Then I can track the base case
13:46How can I track the base case?
13:47The base case is always tracked by parameters
13:48Whatever the parameters increase
13:50We will see the maximum value of it
13:52Or if the parameters increase
13:54We will see the maximum value of it
13:56So I have a base case
13:57Here my row is increasing
14:00With each function call
14:02This is my function call
14:03And this is my backtracking step
14:07This is my backtracking step
14:09This is my backtracking step
14:09Which always comes after function call
14:11And I will put the horizontal length
14:12In the base case
14:13Now I will be increasing
14:14As the row is increasing
14:15And the row is increasing
14:15And the row is increasing
14:17The board is increasing
14:21What is the combination of queens?
14:24After the board, I will return the return
14:26We will also write a function of the board
14:30Public static void print board
14:34I will have a character board
14:37which is a 2D array of characters
14:39I will simply write the 2D array
14:42I will write the print function
14:46board.length i++
14:50for int j equals 0 j less than
14:53board.length j++
14:58I will print every time
15:01board of ij
15:03I will write the next line
15:09I have a function of the board
15:12Now let's summarize it
15:15At the beginning, I had n is equal to 2
15:16and I have made a chess board
15:19I have also initialized
15:20Now I have called n queens
15:21I have passed the initial row as 0
15:25What will n queens do?
15:26n queens will loop a loop
15:28for each column
15:30Each column will be placed in the row
15:33and then for the next rows
15:35we will have a call here
15:36Then when we backtrack
15:38we will go back to our original row
15:40We will take the queen
15:41and put it away
15:43so that we can put the queen
15:46in this case
15:47when I hit the row
15:48value
15:48board.length
15:49and return
15:52Save and run
15:57What happens
15:58I have a chess board
16:01I have a chess board
16:02How can we make it better?
16:05I will print every time
16:06I will print a line
16:08I will print a line
16:11Chess board
16:12For n is equal to 2
16:18I have one chess board
16:18I have a chess board
16:19The first one is blank
16:21The second one is blank
16:25and the second one is blank
16:26The first one is blank
16:27The third one is blank
16:31The third one is blank
16:33in the sixth column
16:34Then there is queen
16:36And in this two queens
16:38I have to start in this 6th column
16:39and J is equal to 1
16:41Now you will see that we have made 4 boards
16:46N by N, N is equal to 2
16:48We have made 2 by 2
16:50These are all boards that are printed
16:52We have made the same in the solution
16:55The combination we have done here is printed
16:59I will write the dot
17:02X will be better in the description
17:06If I write X
17:10This will become a chess board
17:11This will become a chess board
17:13This will become a chess board
17:15This will become a chess board
17:16There will be 4 combinations for 2 by 2
17:19Now I have to do some modifications
17:21What I have done here
17:24I have put all the queens
17:25I have put all the columns
17:27But I have not checked
17:28If I will put this queen
17:30To put this particular column
17:31I have to check conditions
17:37I have to check
17:39I have to check
17:40I have to check
17:41Is it safe to place queen?
17:44Where to place it?
17:45On my board
17:47If I have a row
17:48And a column
17:50What was the row?
17:51I have stored in row and row
17:53I have stored in row and row
17:55In the row
17:58To place the queen
17:59To place the row
18:00To place the queen
18:01I have to analyze this
18:04If this value is true
18:06Then I can place the queen
18:08If I can place the queen
18:09I will not place the queen
18:10If I have to place the queen
18:12If it is safe
18:12How to check the queen
18:14For this
18:15For example
18:15We have to take n is equal to 4
18:17This is chess board
18:19I have to place the chess board
18:20Before I place the queen
18:22And try the queen
18:23If I can place the queen
18:23Here, I cannot place the queen
18:27Now at the second row
18:28I will place the queen
18:30Will stop the queen
18:33How to place the queen
18:34There will not
18:34No casper
18:37I Racers
18:38Not there
18:39If I can place the queen
18:41The queen
18:41Here is not safe
18:41If I can place the queen
18:42And then the queen
18:42Diagonal attack
18:42So, it cannot safe
18:44Other queen
18:45This will be safe如果
18:47I have to drop the server As
18:49I VI
18:49third row पे आ गई क्या मेरी queen को मैं यहां बिठा सकती हूं यहां पर बिठाना safe नहीं होगा
18:53क्योंकि यह वाली queen यहां से attack कर सकती है
18:56second इस वाली place की बात करते हैं यहां बिठाना भी safe नहीं होगा क्योंकि यह वाली queen directly attack
19:01कर सकती है
19:02Can I put it here?
19:04It won't be safe here because this queen can't attack directly here
19:08So I can't put it here
19:10Can I put it here?
19:11Why? Because this queen can't attack here
19:14So this queen is in 3 positions
19:16And this queen can't attack here
19:20And this queen can't put it here
19:21So what can I say?
19:22In this row I can't put a queen in this row
19:24So I will put it wrong
19:26I will put it wrong
19:27Because if I have to put it in N rows
19:30If I have to put it in a row
19:30Then there will be one queen
19:34So I have to put it wrong
19:35So I will put it backtrack here
19:38I have to put it here
19:41I can't put it here
19:43I can't put it here
19:44I can't put it here
19:45So I will put it in the last position
19:47Now I will go down
19:49First of all, I will not put it here
19:51Because it is directly attacking here
19:53Then I will see if I can put it here
19:55I can put it here
19:58When I put it here
19:59Here I can put it here
20:02I can put it here
20:03I can't put it here
20:04Because there are 2 queens directly attacking
20:06One will attack here
20:07And one will attack here
20:09Here I can put it here
20:10And here I can't put it here
20:15I can't put it here
20:17So I can't put my queen here.
20:20So I can't put my queen here.
20:21So I can't put my fourth queen here.
20:23So I realized that something I have wrong.
20:26So I will backtrack.
20:28Backtrack, I will go to this row.
20:30Third row.
20:30Third row, I will turn my queen here.
20:33I mean, put it here.
20:36I will try to put it here.
20:38I will try to put it here.
20:39So here, what are you doing?
20:40This queen is here.
20:43So this is unsafe.
20:44These two places are also unsafe.
20:47So I can't put it here.
20:49I have to backtrack.
20:51I'm not going to go to the right direction.
20:52Backtrack, I came up.
20:54I can't put it here.
20:55I can't put it here.
20:57So there will be no problem in the upper step.
20:59So I will try to backtrack.
21:01Now I will try to backtrack.
21:02I will put the first queen on this index.
21:05I didn't see the right answer.
21:08Now I will put it here.
21:10I will put it here.
21:12This is my j is equal to 0.
21:13This is my j is equal to 2.
21:15This is my j is equal to 3.
21:17So I have put it on j is equal to 1.
21:19Now I will go to next row.
21:21This is also unsafe.
21:23This is also unsafe.
21:24This is also unsafe.
21:25So I can put it here.
21:26I will put the next queen here.
21:29Then I will go to next row.
21:30I will go to next row.
21:31In the next row, I don't know.
21:33I can't attack on this position.
21:35So I can put it here.
21:36Then I will put it next.
21:38Next row is attacking here.
21:40So I can't put it here.
21:43If there is no attack here.
21:45So I can put it here.
21:47What did I do here?
21:49I have successfully placed N queens in N rows.
21:53And how did I place it?
21:54It was safely placed.
21:56I have checked every time.
21:58I have checked it.
21:59After checking that condition,
22:02I have a solution of N queens.
22:05In which you want to take any queen to see.
22:08Then you can't attack any other queen.
22:11So if I want to check if any queen is safe or not.
22:14What do I do?
22:15I will check.
22:18If I am on this board.
22:20Let's say it is any size board.
22:23This is 5x5 board.
22:26And on this board,
22:28I have placed my queen here.
22:30So what do I want to check?
22:32I will check vertically.
22:35Now,
22:37There is no queen in the upper direction.
22:41I don't need to check vertically.
22:43Why do I need to check?
22:43Because I have placed from the upper direction.
22:46So I will be placed here.
22:48Here, there will be a queen.
22:50Now, I have not been able to have the rose.
22:52So I need to check vertically.
22:55Now, I need to check vertically.
22:56Now, I have to check this diagonal.
22:58I need to check left diagonal.
23:01I need to check left diagonal.
23:02Now, I need to check right diagonal.
23:06Now, I have to check right diagonal.
23:18If I have to check left the other queen.
23:19If I am in this third place,
23:25If I have to put the queen in this third direction.
23:33If I have to put the queen in this third direction,
23:36If I will put the queen in this third direction,
23:46So what do I do? I will create a save function in my code
23:50And I will check this function every time I put the queen
23:53Is it safe to put the queen in that position or not
23:58So I will change the same code in the same code
24:01What did I do? I put the queen in row and j column
24:07So I put a condition every time I put it in this save
24:11Is it safe to put the queen in the row and column?
24:17If this function returns true then I will put it
24:22So the steps will come into this if condition
24:28If it is safe then I will perform all the steps
24:31And we will create a function for this save
24:36Public static boolean type is safe
24:40This function will return true
24:41If it is safe to put the queen in row, j
24:45Then it will return false
24:47Here will come my character board
24:52My row and column
24:54This was here because the value of the column
24:57Every time change the column
24:58But because I have checked it for a cell
25:00So I have given the column column
25:01I have given the name of the variable
25:02Now I will check that I will put it in vertically up
25:07And then I will check that
25:10Diagonal left up
25:12And then I will check that
25:15Diagonal right up
25:17So let's see vertically up
25:19Let's see vertically up
25:20I will say vertically up
25:21My column will be the same
25:24If I will check this column
25:26Then my column will be the same
25:28And the row will be the same
25:30And the row will be the same
25:31I will visualize it
25:32I will also visualize it
25:33Here
25:34If I have i, j
25:36Then what will happen
25:37This cell will be i-1, j
25:41This cell will be i-1, j
25:43This cell will be i-2, j
25:44So what I can say
25:46Vertically up direction means
25:47That I will say
25:48In the same column
25:49My column is j
25:50What is my row
25:51What is my row?
25:52It will decrease by 1
25:53And it will decrease
25:55Until I will leave
25:56Until i is equal to 0
25:57To the row
25:58So what I have to do
26:00I have to do a loop
26:00For vertically up check
26:03For int
26:04My row is going
26:06From row-1
26:08I is greater than equal to 0
26:11I minus-1
26:12I will check every time
26:14If my board of
26:16I, column
26:19Already
26:20Queen is already
26:21So I will return
26:23False
26:25Vertically up loop
26:26This loop starts
26:27From row-1
26:28From row-1
26:29And from row-1
26:30From row-1
26:31To row-1
26:31If I, column
26:34Queen is already
26:35Here
26:38Vertically up direction
26:39So I will place
26:40Which queen
26:40I will place
26:42This place
26:42It will not
26:43Because it is unsafe
26:44If it is unsafe
26:45Then I will return
26:45False
26:46This way
26:47Vertically up direction
26:48Now let's talk
26:50Diagonal up
27:12Diagonal up
27:12I will go
27:13I minus-1
27:13What happens
27:14To left
27:14J minus-1
27:16So I minus-1
27:18J minus-1
27:19What happens
27:22I minus-2
27:24J minus-2
27:25What happens
27:25Every time
27:26I will decrease
27:27By one
27:28J will decrease
27:28By one
27:29When I will decrease
27:30When I also
27:310
27:31Or my j
27:330
27:33Or my j
27:340
27:34Then I will go
27:36Left diagonal
27:38I can write
27:39For int
27:41I equals
27:42Row-1
27:43J equals
27:44Colm-1
27:45When I will
27:47I will get
27:48I equal
27:48And
27:49When I will
27:51I will
27:53I will
27:53I will
27:54I will
27:55I will
27:56Check
27:56What I
27:57I will
27:59I minus-1
28:00J minus
28:01I and
28:03J
28:03Have already
28:04Queen
28:05If it is
28:07I will return
28:08False
28:09So I have
28:09Diagonal
28:10Left
28:11Now let's talk about Diagonal Write-up
28:13How will we check this condition?
28:16For this condition, if I have this cell
28:19I, J
28:21What is Diagonal Write-up?
28:26This cell is I-1
28:28J-1
28:29What is this cell?
28:32I-2 J-2
28:35I can say that when I am going to Diagonal Write-up
28:38So every time I have the value of the row
28:41Because I want to go up to the row
28:45I want to go up to the row
28:47So first I, then I-1
28:49Then I-2
28:50Then I-2
28:51And since Diagonal Write-up
28:54So when I move the right
28:56My column is increasing
28:57Then I have J
29:00Then J
29:00Then J-1
29:01Then J-2
29:02So the cell number is I, J
29:05Then I-1
29:07And J-1
29:08I-2
29:09And J-1
29:10I-2
29:10The row will be less
29:11And the column will be increasing
29:12When I go up to the right side diagonal
29:14So let's write the code
29:16For this
29:16For the right diagonal
29:17My loop will go for int
29:19I equals row-1
29:21J equals column-1
29:24The value of I
29:25To be greater than equal to 0
29:26To be greater than equal to 0
29:27So that J
29:28To be greater than or equal to 0
29:29Be less than
29:31my board.length
29:33When I am doing
29:35I, J, I will check the board of I, J, I will return the false, so I checked the three
29:48conditions
29:48in this safe. If vertically there will be no queen, then it will be unsafe. If diagonally
29:56left, then the queen will be unsafe. Diagonally right, then the queen will be unsafe.
30:02If there is no queen in these three cases, then the queen will be safe. Then I will return the
30:09true.
30:10I have written my entire function of this safe. It will be a big code, but logically
30:16it will be very simple. This is the question of N queens and N knights.
30:24I have placed the queen here. You have different knights.
30:28They are different. The queen of n knights will be difficult.
30:33There are number of knights in the night, but the knights will not be safe.
30:37The knights are also called knights. They were called a king.
30:40They are called knights. They are called Shaddin.
30:42So the question of N knights is also different.
30:44You have to check if it is safe when a knight will be placed in any position
30:50But the conditions will be different, we can solve it in the code
30:55It will give you a homework problem, it will be very easy and easy to follow you
31:00It will be very good practice
31:02I checked in this safe, I have checked in 3 different ways
31:05What was it done in N queens?
31:08In N queens, there was an additional condition that would be safe to place this queen or not
31:14There will not be any change in this code
31:17I will give N value to 4 in the main function
31:21I will save and run
31:24What came in the solution?
31:26In the solution, I have two chess boards, 4 by 4
31:29One is the possible solution for N queens
31:31One is the possible solution
31:33Two possible solutions exist in N queens when N value is equal to 4
31:38Here I am equal to N value to 5
31:41So I think
31:42How many?
31:43I have 10 possible solutions
31:46So N is equal to 5
31:47There will be 10 chess boards
31:48This is the solution
31:49This is the solution
31:50This is the solution
31:52This is the solution
31:52In that way
31:53There will be many solutions
31:55Like you will increase the size of the board
31:58N value
31:59There will be more solutions in N queens
32:03In this game
32:03So thank you