using System;
public class TrominoTiling
{
// Define the four L-shaped configurations
private static readonly int[][][] TROMINO_SHAPES = new int[][][]
{
new int[][] { new int[] {0, 0}, new int[] {0, 1}, new int[] {1, 0} }, // ┌
new int[][] { new int[] {0, 0}, new int[] {0, 1}, new int[] {1, 1} }, // ┐
new int[][] { new int[] {0, 0}, new int[] {1, 0}, new int[] {1, 1} }, // └
new int[][] { new int[] {0, 0}, new int[] {1, 0}, new int[] {1, -1} } // ┘
};
private static bool IsValid(int[,] board, int r, int c, int[][] shape, int size)
{
foreach (var offset in shape)
{
int nr = r + offset[0];
int nc = c + offset[1];
if (nr < 0 || nr >= size || nc < 0 || nc >= size || board[nr, nc] != 0)
return false;
}
return true;
}
private static void Place(int[,] board, int r, int c, int[][] shape, int tileId)
{
foreach (var offset in shape)
{
board[r + offset[0], c + offset[1]] = tileId;
}
}
private static void RemoveL(int[,] board, int r, int c, int[][] shape)
{
foreach (var offset in shape)
{
board[r + offset[0], c + offset[1]] = 0;
}
}
private static bool FindNext0(int[,] board, int size, out int r, out int c)
{
for (r = 0; r < size; ++r)
{
for (c = 0; c < size; ++c)
{
if (board[r, c] == 0)
return true;
}
}
r = -1;
c = -1;
return false;
}
// Main recursive function to fill
private static bool TileBoard(int[,] board, int size, int tileId)
{
if (!FindNext0(board, size, out int r, out int c))
return true; // All cells filled successfully
foreach (var shape in TROMINO_SHAPES)
{
if (IsValid(board, r, c, shape, size))
{
Place(board, r, c, shape, tileId);
if (TileBoard(board, size, tileId + 1))
return true; // Found valid solution
RemoveL(board, r, c, shape);
}
}
return false; // No valid placement, backtrack
}
public static int[,] Tiling(int n, int[] missing)
{
int[,] board = new int[n, n];
board[missing[0], missing[1]] = -1;
if (TileBoard(board, n, 1))
return board;
return new int[,] { { -1 } };
}
public static void Main()
{
int n = 4; // Must be 2^k
int[] missing = { 0, 1 };
int[,] grid = Tiling(n, missing);
int rows = grid.GetLength(0);
int cols = grid.GetLength(1);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
Console.Write(grid[i, j] + " ");
}
Console.WriteLine();
}
}
}