Skip to content

feat: Add TilingProblem new algorithm with Junit tests #5746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}