Given an n×n board (where n = 2k and k≥1), with one missing cell, the task is to fill the remaining cells using L-shaped tiles. An L-shaped tile covers 3 cells in a 2x2 grid, with one cell missing. You need to tile the entire board using the L-shaped tiles, ensuring that the missing cell remains uncovered.
Note: There can be multiple valid ways to tile the board based on the position of the missing cell and the order in which tiles are placed. Your solution may output any one of these valid tilings.
Explanation: In this 4×4 grid, the cell at position (0,1) is missing, and the rest using L-shaped tromino tiles, each marked with a unique number, ensuring all other cells are covered without overlapping.
We will exploring all possible ways to place L-shaped tiles on the grid and leaving exactly one predefined missing cell. It begins by scanning the board to find the first empty cell. For that cell, it attempts to place each of the four possible L-shaped tromino configurations. If a placement is valid, it places the tile and recursively proceeds to fill the rest of the board. If a dead-end is reached, the algorithm backtracks by removing the last placed tile and trying the next configuration. This process continues until the board is fully tiled or all possibilities are exhausted.
Output
1 -1 2 2
1 1 3 2
4 3 3 5
4 4 5 5
Time Complexity: O(4(n^2)), Since each recursive call involves trying 4 possible shapes for every empty cell, the complexity grows exponentially with the board size. Auxiliary Space: O(n2), space needed to store the board.
[Expected Approach] Using Divide and Conquer algorithm
If the board size is 2x2, fill the missing cell with a tile number and return.
Divide the board into four quadrants by halving the dimensions of the board (top-left, top-right, bottom-left, bottom-right).
Place an L-shaped tile in the three quadrants that do not contain the missing cell, and number the tiles sequentially. Now all four quadrants have a cell not to be filled. So our problem reduces from (n x n) to (n/2) x (n/2)
Recursively apply the same process to each of the four smaller sub-boards until the base case (2x2 board) is reached.
Why Above Algorithm Works: We can prove working of above algorithm using mathematical induction.
Let the input square be of size n x n (or 2k * 2k) where k≥1.
Base Case: For k=1, the board is a 2×2 square with one missing cell, which can be directly solved by placing an L-shaped tile.
Inductive Hypothesis: Assume the problem can be solved for a square of size 2k-1 * 2k-1 (n/2 x n/2)
Inductive Step: For a square of size 2k * 2k divide it into four smaller 2k-1 * 2k-1 squares. Place an L-shaped tile in the center, covering three quadrants, and leave one quadrant with a missing cell. This reduces the problem to solving the four smaller subproblems, each of size 2k-1 * 2k-1, which is possible by the Induction Hypothesis.
The below diagram shows how we get 4 similar subproblems once we place the L shape in middle such that missing tile quadrant is not changed (1st quadrant in the below example), then our problem reduces to the same problem with smaller size as each quadrant now has a cell which is not supposed to be filled.
Time Complexity: O(n2), where n is the size of the grid. It recursively divides the grid into four quadrants, and each cell is visited and filled exactly once. Auxiliary Space: O(n2)