Given a special Jigsaw puzzle consisting of N rows and M columns all identical pieces. Every piece has three tabs and one blank. The task is to check if the puzzle is solvable by placing the pieces in such a way that the tab of one piece fits perfectly into a blank of other piece.
Note: Rotate and Translate the pieces to solve the puzzle.
Input: N = 2, M = 2
Input: N = 1, M = 3
Approach: The key observation in the problem is that:
- If the Puzzle has only one row or only one column. Then it is possible to solve the puzzle by placing a blank tab on that shared side itself.
- If the Puzzle has two rows and two columns. Then The puzzle is solvable by placing the blank Tabs in a circular chain.
- Otherwise, It is not possible to solve the Puzzle.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.