A matrix is given to you and eight vacant spaces are there. You have to fill the places with the digits 1 to 8 in a way that there are not any adjacent places filled with its next digits
belongs to collection: interview C++ coding problems/challenges | Backtracking
All Answers
total answers (1)
Placing the proper numbers in the right place is a try and error process and to solve it we use the backtracking process. To solve the matrix we have to follow the following steps.
If the matrix is this,
And we start with the vacant place (1,2) and put the number 1 and if we will go (1,3), (2,3), (2,2), (2,4), (2,1) points followed by (1,2) and putting the numbers 3, 7, 2, 5. Then there are two vacant places but we will not fill those places with the numbers 8 or 6.
So using the backtracking method, in the place of (2,3) will put the next number 6 and again applying the same method to fill all the vacant places with the numbers.
C++ implementation:
Output