You are given a special jigsaw puzzle consisting of n⋅m identical pieces. Every piece has three tabs and one blank, as pictured below.
The jigsaw puzzle is considered solved if the following conditions hold: 1. The pieces are arranged into a grid with n rows and m columns. 2. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
We can have blank or tab at the perimeter.image of single piece
I couldn't figure out what to do even after looking into lots of articles. This was asked as a programming problem and I seem to have stuck.