Skip to content

Commit 3401c00

Browse files
authored
Add TilingProblem algorithm (#5746)
1 parent 1e01ec5 commit 3401c00

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
* [MedianOfTwoSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java)
256256
* [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java)
257257
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
258+
* [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
258259
* dynamicprogramming
259260
* [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
260261
* [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
@@ -838,6 +839,7 @@
838839
* [MedianOfTwoSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java)
839840
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
840841
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
842+
* [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
841843
* dynamicprogramming
842844
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
843845
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
// Top-left quadrant
68+
if (missingRow < row + half && missingCol < col + half) {
69+
fillBoard(half, row, col, missingRow, missingCol);
70+
} else {
71+
board[row + half - 1][col + half - 1] = t;
72+
fillBoard(half, row, col, row + half - 1, col + half - 1);
73+
}
74+
75+
// Top-right quadrant
76+
if (missingRow < row + half && missingCol >= col + half) {
77+
fillBoard(half, row, col + half, missingRow, missingCol);
78+
} else {
79+
board[row + half - 1][col + half] = t;
80+
fillBoard(half, row, col + half, row + half - 1, col + half);
81+
}
82+
83+
// Bottom-left quadrant
84+
if (missingRow >= row + half && missingCol < col + half) {
85+
fillBoard(half, row + half, col, missingRow, missingCol);
86+
} else {
87+
board[row + half][col + half - 1] = t;
88+
fillBoard(half, row + half, col, row + half, col + half - 1);
89+
}
90+
91+
// Bottom-right quadrant
92+
if (missingRow >= row + half && missingCol >= col + half) {
93+
fillBoard(half, row + half, col + half, missingRow, missingCol);
94+
} else {
95+
board[row + half][col + half] = t;
96+
fillBoard(half, row + half, col + half, row + half, col + half);
97+
}
98+
}
99+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class TilingProblemTest {
8+
9+
@Test
10+
public void testTilingSize2() {
11+
int[][] expected = {{1, 1}, {1, 0}};
12+
int[][] result = TilingProblem.solveTiling(2, 1, 1);
13+
assertArrayEquals(expected, result);
14+
}
15+
}

0 commit comments

Comments
 (0)