|
| 1 | +package com.thealgorithms.divideandconquer; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class provides a solution to the Tiling Problem using divide-and-conquer. |
| 5 | + * <p> |
| 6 | + * The Tiling Problem involves filling a 2^n x 2^n board with a single missing |
| 7 | + * square using L-shaped tiles (each tile covers exactly three squares). |
| 8 | + * The algorithm recursively divides the board into four quadrants, places an |
| 9 | + * L-shaped tile in the appropriate quadrant, and fills the remaining areas. |
| 10 | + * |
| 11 | + * <p>Applications: |
| 12 | + * - Used in graphics and image processing. |
| 13 | + * - Helpful in solving puzzles and tiling problems in competitive programming. |
| 14 | + * |
| 15 | + * @author Hardvan |
| 16 | + */ |
| 17 | +public final class TilingProblem { |
| 18 | + private TilingProblem() { |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * A counter used to label the L-shaped tiles placed on the board. |
| 23 | + */ |
| 24 | + private static int tile = 1; |
| 25 | + |
| 26 | + /** |
| 27 | + * A 2D array representing the board to be tiled. |
| 28 | + */ |
| 29 | + private static int[][] board; |
| 30 | + |
| 31 | + /** |
| 32 | + * Solves the tiling problem for a 2^n x 2^n board with one missing square. |
| 33 | + * |
| 34 | + * @param size The size of the board (must be a power of 2). |
| 35 | + * @param missingRow The row index of the missing square. |
| 36 | + * @param missingCol The column index of the missing square. |
| 37 | + * @return A 2D array representing the tiled board with L-shaped tiles. |
| 38 | + */ |
| 39 | + public static int[][] solveTiling(int size, int missingRow, int missingCol) { |
| 40 | + board = new int[size][size]; |
| 41 | + fillBoard(size, 0, 0, missingRow, missingCol); |
| 42 | + return board; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Recursively fills the board with L-shaped tiles. |
| 47 | + * |
| 48 | + * <p>The board is divided into four quadrants. Depending on the location of |
| 49 | + * the missing square, an L-shaped tile is placed at the center of the board |
| 50 | + * to cover three of the four quadrants. The process is then repeated for |
| 51 | + * each quadrant until the entire board is filled. |
| 52 | + * |
| 53 | + * @param size The current size of the sub-board. |
| 54 | + * @param row The starting row index of the current sub-board. |
| 55 | + * @param col The starting column index of the current sub-board. |
| 56 | + * @param missingRow The row index of the missing square within the board. |
| 57 | + * @param missingCol The column index of the missing square within the board. |
| 58 | + */ |
| 59 | + private static void fillBoard(int size, int row, int col, int missingRow, int missingCol) { |
| 60 | + if (size == 1) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + int half = size / 2; |
| 65 | + int t = tile++; |
| 66 | + |
| 67 | + |
| 68 | + // Top-left quadrant |
| 69 | + if (missingRow < row + half && missingCol < col + half) { |
| 70 | + fillBoard(half, row, col, missingRow, missingCol); |
| 71 | + } else { |
| 72 | + board[row + half - 1][col + half - 1] = t; |
| 73 | + fillBoard(half, row, col, row + half - 1, col + half - 1); |
| 74 | + } |
| 75 | + |
| 76 | + // Top-right quadrant |
| 77 | + if (missingRow < row + half && missingCol >= col + half) { |
| 78 | + fillBoard(half, row, col + half, missingRow, missingCol); |
| 79 | + } else { |
| 80 | + board[row + half - 1][col + half] = t; |
| 81 | + fillBoard(half, row, col + half, row + half - 1, col + half); |
| 82 | + } |
| 83 | + |
| 84 | + // Bottom-left quadrant |
| 85 | + if (missingRow >= row + half && missingCol < col + half) { |
| 86 | + fillBoard(half, row + half, col, missingRow, missingCol); |
| 87 | + } else { |
| 88 | + board[row + half][col + half - 1] = t; |
| 89 | + fillBoard(half, row + half, col, row + half, col + half - 1); |
| 90 | + } |
| 91 | + |
| 92 | + // Bottom-right quadrant |
| 93 | + if (missingRow >= row + half && missingCol >= col + half) { |
| 94 | + fillBoard(half, row + half, col + half, missingRow, missingCol); |
| 95 | + } else { |
| 96 | + board[row + half][col + half] = t; |
| 97 | + fillBoard(half, row + half, col + half, row + half, col + half); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments