Skip to content

Commit ecd75c0

Browse files
authored
Add CrosswordSolver algorithm (#5626)
1 parent 435532f commit ecd75c0

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [AllPathsFromSourceToTarget](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java)
1111
* [ArrayCombination](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java)
1212
* [Combination](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/Combination.java)
13+
* [CrosswordSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java)
1314
* [FloodFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/FloodFill.java)
1415
* [KnightsTour](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/KnightsTour.java)
1516
* [MazeRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java)
@@ -616,6 +617,7 @@
616617
* [AllPathsFromSourceToTargetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java)
617618
* [ArrayCombinationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java)
618619
* [CombinationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/CombinationTest.java)
620+
* [CrosswordSolverTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/CrosswordSolverTest.java)
619621
* [FloodFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java)
620622
* [KnightsTourTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/KnightsTourTest.java)
621623
* [MazeRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/MazeRecursionTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A class to solve a crossword puzzle using backtracking.
8+
* Example:
9+
* Input:
10+
* puzzle = {
11+
* {' ', ' ', ' '},
12+
* {' ', ' ', ' '},
13+
* {' ', ' ', ' '}
14+
* }
15+
* words = List.of("cat", "dog")
16+
*
17+
* Output:
18+
* {
19+
* {'c', 'a', 't'},
20+
* {' ', ' ', ' '},
21+
* {'d', 'o', 'g'}
22+
* }
23+
*/
24+
public final class CrosswordSolver {
25+
private CrosswordSolver() {
26+
}
27+
28+
/**
29+
* Checks if a word can be placed at the specified position in the crossword.
30+
*
31+
* @param puzzle The crossword puzzle represented as a 2D char array.
32+
* @param word The word to be placed.
33+
* @param row The row index where the word might be placed.
34+
* @param col The column index where the word might be placed.
35+
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
36+
* @return true if the word can be placed, false otherwise.
37+
*/
38+
public static boolean isValid(char[][] puzzle, String word, int row, int col, boolean vertical) {
39+
for (int i = 0; i < word.length(); i++) {
40+
if (vertical) {
41+
if (row + i >= puzzle.length || puzzle[row + i][col] != ' ') {
42+
return false;
43+
}
44+
} else {
45+
if (col + i >= puzzle[0].length || puzzle[row][col + i] != ' ') {
46+
return false;
47+
}
48+
}
49+
}
50+
return true;
51+
}
52+
53+
/**
54+
* Places a word at the specified position in the crossword.
55+
*
56+
* @param puzzle The crossword puzzle represented as a 2D char array.
57+
* @param word The word to be placed.
58+
* @param row The row index where the word will be placed.
59+
* @param col The column index where the word will be placed.
60+
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
61+
*/
62+
public static void placeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
63+
for (int i = 0; i < word.length(); i++) {
64+
if (vertical) {
65+
puzzle[row + i][col] = word.charAt(i);
66+
} else {
67+
puzzle[row][col + i] = word.charAt(i);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Removes a word from the specified position in the crossword.
74+
*
75+
* @param puzzle The crossword puzzle represented as a 2D char array.
76+
* @param word The word to be removed.
77+
* @param row The row index where the word is placed.
78+
* @param col The column index where the word is placed.
79+
* @param vertical If true, the word was placed vertically; otherwise, horizontally.
80+
*/
81+
public static void removeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
82+
for (int i = 0; i < word.length(); i++) {
83+
if (vertical) {
84+
puzzle[row + i][col] = ' ';
85+
} else {
86+
puzzle[row][col + i] = ' ';
87+
}
88+
}
89+
}
90+
91+
/**
92+
* Solves the crossword puzzle using backtracking.
93+
*
94+
* @param puzzle The crossword puzzle represented as a 2D char array.
95+
* @param words The list of words to be placed.
96+
* @return true if the crossword is solved, false otherwise.
97+
*/
98+
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
// Create a mutable copy of the words list
100+
List<String> remainingWords = new ArrayList<>(words);
101+
102+
for (int row = 0; row < puzzle.length; row++) {
103+
for (int col = 0; col < puzzle[0].length; col++) {
104+
if (puzzle[row][col] == ' ') {
105+
for (String word : new ArrayList<>(remainingWords)) {
106+
for (boolean vertical : new boolean[] {true, false}) {
107+
if (isValid(puzzle, word, row, col, vertical)) {
108+
placeWord(puzzle, word, row, col, vertical);
109+
remainingWords.remove(word);
110+
if (solveCrossword(puzzle, remainingWords)) {
111+
return true;
112+
}
113+
remainingWords.add(word);
114+
removeWord(puzzle, word, row, col, vertical);
115+
}
116+
}
117+
}
118+
return false;
119+
}
120+
}
121+
}
122+
return true;
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class CrosswordSolverTest {
12+
13+
@Test
14+
public void testValidPlacement() {
15+
char[][] puzzle = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
16+
assertTrue(CrosswordSolver.isValid(puzzle, "cat", 0, 0, true));
17+
assertTrue(CrosswordSolver.isValid(puzzle, "dog", 0, 0, false));
18+
assertFalse(CrosswordSolver.isValid(puzzle, "cat", 1, 2, false));
19+
}
20+
21+
@Test
22+
public void testPlaceAndRemoveWord() {
23+
char[][] puzzle = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
24+
CrosswordSolver.placeWord(puzzle, "cat", 0, 0, true);
25+
assertEquals('c', puzzle[0][0]);
26+
assertEquals('a', puzzle[1][0]);
27+
assertEquals('t', puzzle[2][0]);
28+
29+
CrosswordSolver.removeWord(puzzle, "cat", 0, 0, true);
30+
assertEquals(' ', puzzle[0][0]);
31+
assertEquals(' ', puzzle[1][0]);
32+
assertEquals(' ', puzzle[2][0]);
33+
}
34+
35+
@Test
36+
public void testSolveCrossword() {
37+
char[][] puzzle = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
38+
List<String> words = Arrays.asList("cat", "dog", "car");
39+
assertTrue(CrosswordSolver.solveCrossword(puzzle, words));
40+
41+
/* Solved crossword:
42+
* c d c
43+
* a o a
44+
* t g r
45+
*/
46+
47+
assertEquals('c', puzzle[0][0]);
48+
assertEquals('a', puzzle[1][0]);
49+
assertEquals('t', puzzle[2][0]);
50+
51+
assertEquals('d', puzzle[0][1]);
52+
assertEquals('o', puzzle[1][1]);
53+
assertEquals('g', puzzle[2][1]);
54+
55+
assertEquals('c', puzzle[0][2]);
56+
assertEquals('a', puzzle[1][2]);
57+
assertEquals('r', puzzle[2][2]);
58+
}
59+
60+
@Test
61+
public void testNoSolution() {
62+
char[][] puzzle = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
63+
List<String> words = Arrays.asList("cat", "dog", "elephant"); // 'elephant' is too long for the grid
64+
assertFalse(CrosswordSolver.solveCrossword(puzzle, words));
65+
}
66+
}

0 commit comments

Comments
 (0)