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.
C++
#include<iostream>#include<vector>usingnamespacestd;// Define the four L-shaped configurationsconstvector<vector<pair<int,int>>>TROMINO_SHAPES={{{0,0},{0,1},{1,0}},// ┌{{0,0},{0,1},{1,1}},// ┐{{0,0},{1,0},{1,1}},// └{{0,0},{1,0},{1,-1}}// ┘};boolisValid(vector<vector<int>>&board,intr,intc,constvector<pair<int,int>>&shape,intsize){for(constauto&offset:shape){intnr=r+offset.first;intnc=c+offset.second;if(nr<0||nr>=size||nc<0||nc>=size||board[nr][nc]!=0)returnfalse;}returntrue;}voidplace(vector<vector<int>>&board,intr,intc,constvector<pair<int,int>>&shape,inttileId){for(constauto&offset:shape){board[r+offset.first][c+offset.second]=tileId;}}voidremoveL(vector<vector<int>>&board,intr,intc,constvector<pair<int,int>>&shape){for(constauto&offset:shape){board[r+offset.first][c+offset.second]=0;}}boolfindNext0(vector<vector<int>>&board,intsize,int&outR,int&outC){for(intr=0;r<size;++r){for(intc=0;c<size;++c){if(board[r][c]==0){outR=r;outC=c;returntrue;}}}returnfalse;}// Main recursive function to fillbooltileBoard(vector<vector<int>>&board,intsize,inttileId){intr,c;if(!findNext0(board,size,r,c)){returntrue;// All cells filled successfully}for(constauto&shape:TROMINO_SHAPES){if(isValid(board,r,c,shape,size)){place(board,r,c,shape,tileId);if(tileBoard(board,size,tileId+1)){returntrue;// Found valid solution}removeL(board,r,c,shape);}}returnfalse;// No valid placement, backtrack}vector<vector<int>>tiling(intn,pair<int,int>missing){vector<vector<int>>board(n,vector<int>(n,0));board[missing.first][missing.second]=-1;if(tileBoard(board,n,1))returnboard;return{{-1}};}intmain(){intn=4;// Must be 2^kpair<int,int>missing={0,1};vector<vector<int>>grid=tiling(n,missing);for(constauto&row:grid){for(intcell:row){cout<<cell<<" ";}cout<<"\n";}return0;}
Java
// Import necessary packagesimportjava.util.*;publicclassTrominoTiling{// Define the four L-shaped configurationsprivatestaticfinalint[][][]TROMINO_SHAPES={{{0,0},{0,1},{1,0}},// ┌{{0,0},{0,1},{1,1}},// ┐{{0,0},{1,0},{1,1}},// └{{0,0},{1,0},{1,-1}}// ┘};privatestaticbooleanisValid(int[][]board,intr,intc,int[][]shape,intsize){for(int[]offset:shape){intnr=r+offset[0];intnc=c+offset[1];if(nr<0||nr>=size||nc<0||nc>=size||board[nr][nc]!=0)returnfalse;}returntrue;}privatestaticvoidplace(int[][]board,intr,intc,int[][]shape,inttileId){for(int[]offset:shape){board[r+offset[0]][c+offset[1]]=tileId;}}privatestaticvoidremoveL(int[][]board,intr,intc,int[][]shape){for(int[]offset:shape){board[r+offset[0]][c+offset[1]]=0;}}privatestaticbooleanfindNext0(int[][]board,intsize,int[]out){for(intr=0;r<size;++r){for(intc=0;c<size;++c){if(board[r][c]==0){out[0]=r;out[1]=c;returntrue;}}}returnfalse;}// Main recursive function to fillprivatestaticbooleantileBoard(int[][]board,intsize,inttileId){int[]pos=newint[2];if(!findNext0(board,size,pos)){returntrue;// All cells filled successfully}intr=pos[0];intc=pos[1];for(int[][]shape:TROMINO_SHAPES){if(isValid(board,r,c,shape,size)){place(board,r,c,shape,tileId);if(tileBoard(board,size,tileId+1)){returntrue;// Found valid solution}removeL(board,r,c,shape);}}returnfalse;// No valid placement, backtrack}publicstaticint[][]tiling(intn,int[]missing){int[][]board=newint[n][n];board[missing[0]][missing[1]]=-1;if(tileBoard(board,n,1))returnboard;returnnewint[][]{{-1}};}publicstaticvoidmain(String[]args){intn=4;// Must be 2^kint[]missing={0,1};int[][]grid=tiling(n,missing);for(int[]row:grid){for(intcell:row){System.out.print(cell+" ");}System.out.println();}}}
Python
# Define the four L-shaped configurationsTROMINO_SHAPES=[[(0,0),(0,1),(1,0)],# ┌[(0,0),(0,1),(1,1)],# ┐[(0,0),(1,0),(1,1)],# └[(0,0),(1,0),(1,-1)]# ┘]defis_valid(board,r,c,shape,size):foroffsetinshape:nr=r+offset[0]nc=c+offset[1]ifnr<0ornr>=sizeornc<0ornc>=sizeorboard[nr][nc]!=0:returnFalsereturnTruedefplace(board,r,c,shape,tile_id):foroffsetinshape:board[r+offset[0]][c+offset[1]]=tile_iddefremove_l(board,r,c,shape):foroffsetinshape:board[r+offset[0]][c+offset[1]]=0deffind_next_0(board,size):forrinrange(size):forcinrange(size):ifboard[r][c]==0:returnr,creturnNone,None# Main recursive function to filldeftile_board(board,size,tile_id):r,c=find_next_0(board,size)ifrisNone:returnTrue# All cells filled successfullyforshapeinTROMINO_SHAPES:ifis_valid(board,r,c,shape,size):place(board,r,c,shape,tile_id)iftile_board(board,size,tile_id+1):returnTrue# Found valid solutionremove_l(board,r,c,shape)returnFalse# No valid placement, backtrackdeftiling(n,missing):board=[[0]*nfor_inrange(n)]board[missing[0]][missing[1]]=-1iftile_board(board,n,1):returnboardreturn[[-1]]if__name__=='__main__':n=4# Must be 2^kmissing=(0,1)grid=tiling(n,missing)forrowingrid:print(' '.join(map(str,row)))
C#
usingSystem;publicclassTrominoTiling{// Define the four L-shaped configurationsprivatestaticreadonlyint[][][]TROMINO_SHAPES=newint[][][]{newint[][]{newint[]{0,0},newint[]{0,1},newint[]{1,0}},// ┌newint[][]{newint[]{0,0},newint[]{0,1},newint[]{1,1}},// ┐newint[][]{newint[]{0,0},newint[]{1,0},newint[]{1,1}},// └newint[][]{newint[]{0,0},newint[]{1,0},newint[]{1,-1}}// ┘};privatestaticboolIsValid(int[,]board,intr,intc,int[][]shape,intsize){foreach(varoffsetinshape){intnr=r+offset[0];intnc=c+offset[1];if(nr<0||nr>=size||nc<0||nc>=size||board[nr,nc]!=0)returnfalse;}returntrue;}privatestaticvoidPlace(int[,]board,intr,intc,int[][]shape,inttileId){foreach(varoffsetinshape){board[r+offset[0],c+offset[1]]=tileId;}}privatestaticvoidRemoveL(int[,]board,intr,intc,int[][]shape){foreach(varoffsetinshape){board[r+offset[0],c+offset[1]]=0;}}privatestaticboolFindNext0(int[,]board,intsize,outintr,outintc){for(r=0;r<size;++r){for(c=0;c<size;++c){if(board[r,c]==0)returntrue;}}r=-1;c=-1;returnfalse;}// Main recursive function to fillprivatestaticboolTileBoard(int[,]board,intsize,inttileId){if(!FindNext0(board,size,outintr,outintc))returntrue;// All cells filled successfullyforeach(varshapeinTROMINO_SHAPES){if(IsValid(board,r,c,shape,size)){Place(board,r,c,shape,tileId);if(TileBoard(board,size,tileId+1))returntrue;// Found valid solutionRemoveL(board,r,c,shape);}}returnfalse;// No valid placement, backtrack}publicstaticint[,]Tiling(intn,int[]missing){int[,]board=newint[n,n];board[missing[0],missing[1]]=-1;if(TileBoard(board,n,1))returnboard;returnnewint[,]{{-1}};}publicstaticvoidMain(){intn=4;// Must be 2^kint[]missing={0,1};int[,]grid=Tiling(n,missing);introws=grid.GetLength(0);intcols=grid.GetLength(1);for(inti=0;i<rows;++i){for(intj=0;j<cols;++j){Console.Write(grid[i,j]+" ");}Console.WriteLine();}}}
JavaScript
constTROMINO_SHAPES=[[[0,0],[0,1],[1,0]],// ┌[[0,0],[0,1],[1,1]],// ┐[[0,0],[1,0],[1,1]],// └[[0,0],[1,0],[1,-1]]// ┘];functionisValid(board,r,c,shape,size){for(constoffsetofshape){constnr=r+offset[0];constnc=c+offset[1];if(nr<0||nr>=size||nc<0||nc>=size||board[nr][nc]!==0){returnfalse;}}returntrue;}functionplace(board,r,c,shape,tileId){for(constoffsetofshape){board[r+offset[0]][c+offset[1]]=tileId;}}functionremoveL(board,r,c,shape){for(constoffsetofshape){board[r+offset[0]][c+offset[1]]=0;}}functionfindNextZero(board,size){for(letr=0;r<size;r++){for(letc=0;c<size;c++){if(board[r][c]===0){return[r,c];}}}return[null,null];}functiontileBoard(board,size,tileId){const[r,c]=findNextZero(board,size);if(r===null){returntrue;// All cells filled successfully}for(constshapeofTROMINO_SHAPES){if(isValid(board,r,c,shape,size)){place(board,r,c,shape,tileId);if(tileBoard(board,size,tileId+1)){returntrue;// Found valid solution}removeL(board,r,c,shape);// Backtrack}}returnfalse;// No valid placement}functiontiling(n,missing){constboard=Array.from({length:n},()=>Array(n).fill(0));board[missing[0]][missing[1]]=-1;// Mark missing squareif(tileBoard(board,n,1)){returnboard;}return[[-1]];}// Example usage:constn=4;// Must be 2^kconstmissing=[0,1];constgrid=tiling(n,missing);for(constrowofgrid){console.log(row.join(' '));}
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.
Subproblems for Divide and Conquer{
Illustration:
C++
#include<bits/stdc++.h>usingnamespacestd;// Tile number to be filledintt=0;// Function to place tiles in the sub-gridsvoidplace_tile(intx1,inty1,intx2,inty2,intx3,inty3,vector<vector<int>>&grid){t++;grid[x1][y1]=t;grid[x2][y2]=t;grid[x3][y3]=t;}// Recursive function to fill the gridvoidsolve(intsz,intr,intc,vector<vector<int>>&grid){// Base case: when the grid size is 2x2if(sz==2){t++;for(inti=0;i<sz;i++)for(intj=0;j<sz;j++)if(grid[r+i][c+j]==0)grid[r+i][c+j]=t;return;}// To store the missing cell's coordinatesintmr,mc;// Find the missing cell within the current sub-gridfor(inti=r;i<r+sz;i++)for(intj=c;j<c+sz;j++)if(grid[i][j]!=0)mr=i,mc=j;// Place tiles based on the quadrant where the missing cell is located// First quadrantif(mr<r+sz/2&&mc<c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2,grid);// Second quadrantelseif(mr>=r+sz/2&&mc<c+sz/2)place_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Third quadrantelseif(mr<r+sz/2&&mc>=c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Fourth quadrantelseplace_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2-1,r+sz/2-1,c+sz/2-1,grid);// Recursively solve for the 4 sub-grids// Top-rightsolve(sz/2,r,c+sz/2,grid);// Top-leftsolve(sz/2,r,c,grid);// Bottom-leftsolve(sz/2,r+sz/2,c,grid);// Bottom-rightsolve(sz/2,r+sz/2,c+sz/2,grid);}vector<vector<int>>tiling(intn,pair<int,int>missing_cell){vector<vector<int>>grid(n,vector<int>(n,0));grid[missing_cell.first][missing_cell.second]=-1;solve(n,0,0,grid);returngrid;}intmain(){intn=4;pair<int,int>missing_cell={0,1};vector<vector<int>>grid=tiling(n,missing_cell);for(constauto&row:grid){for(intcell:row)cout<<cell<<" ";cout<<endl;}return0;}
Java
importjava.util.*;publicclassMain{// Tile number to be filledstaticintt=0;// Function to place tiles in the sub-gridsstaticvoidplace_tile(intx1,inty1,intx2,inty2,intx3,inty3,int[][]grid){t++;grid[x1][y1]=t;grid[x2][y2]=t;grid[x3][y3]=t;}// Recursive function to fill the gridstaticvoidsolve(intsz,intr,intc,int[][]grid){// Base case: when the grid size is 2x2if(sz==2){t++;for(inti=0;i<sz;i++)for(intj=0;j<sz;j++)if(grid[r+i][c+j]==0)grid[r+i][c+j]=t;return;}// To store the missing cell's coordinatesintmr=-1,mc=-1;// Find the missing cell within the current sub-gridfor(inti=r;i<r+sz;i++)for(intj=c;j<c+sz;j++)if(grid[i][j]!=0){mr=i;mc=j;}// First quadrantif(mr<r+sz/2&&mc<c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2,grid);// Second quadrantelseif(mr>=r+sz/2&&mc<c+sz/2)place_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Third quadrantelseif(mr<r+sz/2&&mc>=c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Fourth quadrantelseplace_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2-1,r+sz/2-1,c+sz/2-1,grid);// Top-rightsolve(sz/2,r,c+sz/2,grid);// Top-leftsolve(sz/2,r,c,grid);// Bottom-leftsolve(sz/2,r+sz/2,c,grid);// Bottom-rightsolve(sz/2,r+sz/2,c+sz/2,grid);}// Function to start the tiling processstaticint[][]tiling(intn,int[]missing_cell){int[][]grid=newint[n][n];grid[missing_cell[0]][missing_cell[1]]=-1;solve(n,0,0,grid);returngrid;}// Main method to run the programpublicstaticvoidmain(String[]args){intn=4;int[]missing_cell={0,1};int[][]grid=tiling(n,missing_cell);for(int[]row:grid){for(intcell:row)System.out.print(cell+" ");System.out.println();}}}
Python
# Tile number to be filledt=0# Function to place tiles in the sub-gridsdefplace_tile(x1,y1,x2,y2,x3,y3,grid):globaltt+=1grid[x1][y1]=tgrid[x2][y2]=tgrid[x3][y3]=t# Recursive function to fill the griddefsolve(sz,r,c,grid):globalt# Base case: when the grid size is 2x2ifsz==2:t+=1foriinrange(sz):forjinrange(sz):ifgrid[r+i][c+j]==0:grid[r+i][c+j]=treturn# To store the missing cell's coordinatesmr,mc=-1,-1# Find the missing cell within the current sub-gridforiinrange(r,r+sz):forjinrange(c,c+sz):ifgrid[i][j]!=0:mr,mc=i,j# First quadrantifmr<r+sz//2andmc<c+sz//2:place_tile(r+sz//2,c+sz//2-1,r+sz//2,c+sz//2,r+sz//2-1,c+sz//2,grid)# Second quadrantelifmr>=r+sz//2andmc<c+sz//2:place_tile(r+sz//2-1,c+sz//2,r+sz//2,c+sz//2,r+sz//2-1,c+sz//2-1,grid)# Third quadrantelifmr<r+sz//2andmc>=c+sz//2:place_tile(r+sz//2,c+sz//2-1,r+sz//2,c+sz//2,r+sz//2-1,c+sz//2-1,grid)# Fourth quadrantelse:place_tile(r+sz//2-1,c+sz//2,r+sz//2,c+sz//2-1,r+sz//2-1,c+sz//2-1,grid)# Top-rightsolve(sz//2,r,c+sz//2,grid)# Top-leftsolve(sz//2,r,c,grid)# Bottom-leftsolve(sz//2,r+sz//2,c,grid)# Bottom-rightsolve(sz//2,r+sz//2,c+sz//2,grid)# Function to start the tiling processdeftiling(n,missing_cell):grid=[[0for_inrange(n)]for_inrange(n)]grid[missing_cell[0]][missing_cell[1]]=-1solve(n,0,0,grid)returngrid# Main executionn=4missing_cell=(0,1)grid=tiling(n,missing_cell)forrowingrid:print(' '.join(str(cell)forcellinrow))
C#
usingSystem;usingSystem.Collections.Generic;publicclassProgram{// Tile number to be filledstaticintt=0;// Function to place tiles in the sub-gridsstaticvoidplace_tile(intx1,inty1,intx2,inty2,intx3,inty3,int[,]grid){t++;grid[x1,y1]=t;grid[x2,y2]=t;grid[x3,y3]=t;}// Recursive function to fill the gridstaticvoidsolve(intsz,intr,intc,int[,]grid){// Base case: when the grid size is 2x2if(sz==2){t++;for(inti=0;i<sz;i++)for(intj=0;j<sz;j++)if(grid[r+i,c+j]==0)grid[r+i,c+j]=t;return;}// To store the missing cell's coordinatesintmr=-1,mc=-1;// Find the missing cell within the current sub-gridfor(inti=r;i<r+sz;i++)for(intj=c;j<c+sz;j++)if(grid[i,j]!=0){mr=i;mc=j;}// First quadrantif(mr<r+sz/2&&mc<c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2,grid);// Second quadrantelseif(mr>=r+sz/2&&mc<c+sz/2)place_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Third quadrantelseif(mr<r+sz/2&&mc>=c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Fourth quadrantelseplace_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2-1,r+sz/2-1,c+sz/2-1,grid);// Top-rightsolve(sz/2,r,c+sz/2,grid);// Top-leftsolve(sz/2,r,c,grid);// Bottom-leftsolve(sz/2,r+sz/2,c,grid);// Bottom-rightsolve(sz/2,r+sz/2,c+sz/2,grid);}// Function to start the tiling processstaticList<int[]>tiling(intn,int[]missing_cell){int[,]grid=newint[n,n];grid[missing_cell[0],missing_cell[1]]=-1;solve(n,0,0,grid);List<int[]>result=newList<int[]>();// Convert 2D array to List<int[]>for(inti=0;i<n;i++){int[]row=newint[n];for(intj=0;j<n;j++){row[j]=grid[i,j];}result.Add(row);}returnresult;}// Main method to run the programpublicstaticvoidMain(){intn=4;int[]missing_cell={0,1};List<int[]>grid=tiling(n,missing_cell);foreach(varrowingrid){foreach(varcellinrow)Console.Write(cell+" ");Console.WriteLine();}}}
JavaScript
// Tile number to be filledlett=0;// Function to place tiles in the sub-gridsfunctionplace_tile(x1,y1,x2,y2,x3,y3,grid){t++;grid[x1][y1]=t;grid[x2][y2]=t;grid[x3][y3]=t;}// Recursive function to fill the gridfunctionsolve(sz,r,c,grid){// Base case: when the grid size is 2x2if(sz===2){t++;for(leti=0;i<sz;i++){for(letj=0;j<sz;j++){if(grid[r+i][c+j]===0){grid[r+i][c+j]=t;}}}return;}// To store the missing cell's coordinatesletmr=-1,mc=-1;// Find the missing cell within the current sub-gridfor(leti=r;i<r+sz;i++){for(letj=c;j<c+sz;j++){if(grid[i][j]!==0){mr=i;mc=j;}}}// First quadrantif(mr<r+sz/2&&mc<c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2,grid);// Second quadrantelseif(mr>=r+sz/2&&mc<c+sz/2)place_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Third quadrantelseif(mr<r+sz/2&&mc>=c+sz/2)place_tile(r+sz/2,c+sz/2-1,r+sz/2,c+sz/2,r+sz/2-1,c+sz/2-1,grid);// Fourth quadrantelseplace_tile(r+sz/2-1,c+sz/2,r+sz/2,c+sz/2-1,r+sz/2-1,c+sz/2-1,grid);// Top-rightsolve(sz/2,r,c+sz/2,grid);// Top-leftsolve(sz/2,r,c,grid);// Bottom-leftsolve(sz/2,r+sz/2,c,grid);// Bottom-rightsolve(sz/2,r+sz/2,c+sz/2,grid);}// Function to start the tiling processfunctiontiling(n,missing_cell){constgrid=Array.from({length:n},()=>Array(n).fill(0));grid[missing_cell[0]][missing_cell[1]]=-1;solve(n,0,0,grid);returngrid;}// Main executionconstn=4;constmissing_cell=[0,1];constgrid=tiling(n,missing_cell);for(constrowofgrid){console.log(row.join(" "));}
Output
3 -1 2 2
3 3 1 2
4 1 1 5
4 4 5 5
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)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.