Add TilingProblem algorithm (#5746)

This commit is contained in:
Hardik Pawar
2024-10-14 12:41:21 +05:30
committed by GitHub
parent 1e01ec5233
commit 3401c003bd
3 changed files with 116 additions and 0 deletions

View File

@ -255,6 +255,7 @@
* [MedianOfTwoSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java)
* [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java)
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
* [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
* dynamicprogramming
* [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
* [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
@ -838,6 +839,7 @@
* [MedianOfTwoSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java)
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
* [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
* dynamicprogramming
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)

View File

@ -0,0 +1,99 @@
package com.thealgorithms.divideandconquer;
/**
* This class provides a solution to the Tiling Problem using divide-and-conquer.
* <p>
* The Tiling Problem involves filling a 2^n x 2^n board with a single missing
* square using L-shaped tiles (each tile covers exactly three squares).
* The algorithm recursively divides the board into four quadrants, places an
* L-shaped tile in the appropriate quadrant, and fills the remaining areas.
*
* <p>Applications:
* - Used in graphics and image processing.
* - Helpful in solving puzzles and tiling problems in competitive programming.
*
* @author Hardvan
*/
public final class TilingProblem {
private TilingProblem() {
}
/**
* A counter used to label the L-shaped tiles placed on the board.
*/
private static int tile = 1;
/**
* A 2D array representing the board to be tiled.
*/
private static int[][] board;
/**
* Solves the tiling problem for a 2^n x 2^n board with one missing square.
*
* @param size The size of the board (must be a power of 2).
* @param missingRow The row index of the missing square.
* @param missingCol The column index of the missing square.
* @return A 2D array representing the tiled board with L-shaped tiles.
*/
public static int[][] solveTiling(int size, int missingRow, int missingCol) {
board = new int[size][size];
fillBoard(size, 0, 0, missingRow, missingCol);
return board;
}
/**
* Recursively fills the board with L-shaped tiles.
*
* <p>The board is divided into four quadrants. Depending on the location of
* the missing square, an L-shaped tile is placed at the center of the board
* to cover three of the four quadrants. The process is then repeated for
* each quadrant until the entire board is filled.
*
* @param size The current size of the sub-board.
* @param row The starting row index of the current sub-board.
* @param col The starting column index of the current sub-board.
* @param missingRow The row index of the missing square within the board.
* @param missingCol The column index of the missing square within the board.
*/
private static void fillBoard(int size, int row, int col, int missingRow, int missingCol) {
if (size == 1) {
return;
}
int half = size / 2;
int t = tile++;
// Top-left quadrant
if (missingRow < row + half && missingCol < col + half) {
fillBoard(half, row, col, missingRow, missingCol);
} else {
board[row + half - 1][col + half - 1] = t;
fillBoard(half, row, col, row + half - 1, col + half - 1);
}
// Top-right quadrant
if (missingRow < row + half && missingCol >= col + half) {
fillBoard(half, row, col + half, missingRow, missingCol);
} else {
board[row + half - 1][col + half] = t;
fillBoard(half, row, col + half, row + half - 1, col + half);
}
// Bottom-left quadrant
if (missingRow >= row + half && missingCol < col + half) {
fillBoard(half, row + half, col, missingRow, missingCol);
} else {
board[row + half][col + half - 1] = t;
fillBoard(half, row + half, col, row + half, col + half - 1);
}
// Bottom-right quadrant
if (missingRow >= row + half && missingCol >= col + half) {
fillBoard(half, row + half, col + half, missingRow, missingCol);
} else {
board[row + half][col + half] = t;
fillBoard(half, row + half, col + half, row + half, col + half);
}
}
}

View File

@ -0,0 +1,15 @@
package com.thealgorithms.divideandconquer;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class TilingProblemTest {
@Test
public void testTilingSize2() {
int[][] expected = {{1, 1}, {1, 0}};
int[][] result = TilingProblem.solveTiling(2, 1, 1);
assertArrayEquals(expected, result);
}
}