Unit 2
Unit 2
Unit 2
o N-queen problem
o Sum of subset problem
o Graph coloring
o Hamiliton cycle
Step 2:
Put our next Queen (Q2) in the (1,2) cell .
After this move to the next row [ 1 -> 2 ].
Step 3:
At row 2 there is no cell which are safe to place Queen (Q3) .
So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .
Step 4:
There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).
Put Queen ( Q2 ) at cell ( 1, 3).
Step 5:
Put queen ( Q3 ) at cell ( 2, 1 ).
Step 6:
There is no any cell to place Queen ( Q4 ) at row 3.
Backtrack and remove Queen ( Q3 ) from row 2.
Again there is no other safe cell in row 2, So backtrack again
and remove queen ( Q2 ) from row 1.
Queen ( Q1 ) will be remove from cell (0,0) and move to next
safe cell i.e. (0 , 1).
Step 7:
Place Queen Q1 at cell (0 , 1), and move to next row.
Step 8:
Place Queen Q2 at cell (1 , 3), and move to next row.
Step 9:
Place Queen Q3 at cell (2 , 0), and move to next row.
Step 10:
Place Queen Q4 at cell (3 , 2), and move to next row.
This is one possible configuration of solution
Follow the steps below to implement the idea:
Make a recursive function that takes the state of the
column
o Then mark this [row, column] as