-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathCrosswordSolver.java
125 lines (118 loc) · 4.46 KB
/
CrosswordSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.thealgorithms.backtracking;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* A class to solve a crossword puzzle using backtracking.
* Example:
* Input:
* puzzle = {
* {' ', ' ', ' '},
* {' ', ' ', ' '},
* {' ', ' ', ' '}
* }
* words = List.of("cat", "dog")
*
* Output:
* {
* {'c', 'a', 't'},
* {' ', ' ', ' '},
* {'d', 'o', 'g'}
* }
*/
public final class CrosswordSolver {
private CrosswordSolver() {
}
/**
* Checks if a word can be placed at the specified position in the crossword.
*
* @param puzzle The crossword puzzle represented as a 2D char array.
* @param word The word to be placed.
* @param row The row index where the word might be placed.
* @param col The column index where the word might be placed.
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
* @return true if the word can be placed, false otherwise.
*/
public static boolean isValid(char[][] puzzle, String word, int row, int col, boolean vertical) {
for (int i = 0; i < word.length(); i++) {
if (vertical) {
if (row + i >= puzzle.length || puzzle[row + i][col] != ' ') {
return false;
}
} else {
if (col + i >= puzzle[0].length || puzzle[row][col + i] != ' ') {
return false;
}
}
}
return true;
}
/**
* Places a word at the specified position in the crossword.
*
* @param puzzle The crossword puzzle represented as a 2D char array.
* @param word The word to be placed.
* @param row The row index where the word will be placed.
* @param col The column index where the word will be placed.
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
*/
public static void placeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
for (int i = 0; i < word.length(); i++) {
if (vertical) {
puzzle[row + i][col] = word.charAt(i);
} else {
puzzle[row][col + i] = word.charAt(i);
}
}
}
/**
* Removes a word from the specified position in the crossword.
*
* @param puzzle The crossword puzzle represented as a 2D char array.
* @param word The word to be removed.
* @param row The row index where the word is placed.
* @param col The column index where the word is placed.
* @param vertical If true, the word was placed vertically; otherwise, horizontally.
*/
public static void removeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
for (int i = 0; i < word.length(); i++) {
if (vertical) {
puzzle[row + i][col] = ' ';
} else {
puzzle[row][col + i] = ' ';
}
}
}
/**
* Solves the crossword puzzle using backtracking.
*
* @param puzzle The crossword puzzle represented as a 2D char array.
* @param words The list of words to be placed.
* @return true if the crossword is solved, false otherwise.
*/
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
// Create a mutable copy of the words list
List<String> remainingWords = new ArrayList<>(words);
for (int row = 0; row < puzzle.length; row++) {
for (int col = 0; col < puzzle[0].length; col++) {
if (puzzle[row][col] == ' ') {
for (String word : new ArrayList<>(remainingWords)) {
for (boolean vertical : new boolean[] {true, false}) {
if (isValid(puzzle, word, row, col, vertical)) {
placeWord(puzzle, word, row, col, vertical);
remainingWords.remove(word);
if (solveCrossword(puzzle, remainingWords)) {
return true;
}
remainingWords.add(word);
removeWord(puzzle, word, row, col, vertical);
}
}
}
return false;
}
}
}
return true;
}
}