-
Notifications
You must be signed in to change notification settings - Fork 79
/
CrosswordPuzzle.java
389 lines (352 loc) · 11.4 KB
/
CrosswordPuzzle.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
import java.io.*;
//https://www.hackerrank.com/challenges/crossword-puzzle
@SuppressWarnings("unchecked")
public class Solution {
private static final char MINUS = '-';
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
/*String[] sInput = { "+-++++++++",
"+-++++++++",
"+-++++++++",
"+-----++++",
"+-+++-++++",
"+-+++-++++",
"+++++-++++",
"++------++",
"+++++-++++",
"+++++-++++" };*/
/*String[] sInput = {
"+-++++++++",
"+-++++++++",
"+-------++",
"+-++++++++",
"+-----++++",
"+-+++-++++",
"+++-----++",
"+++++-++++",
"+++++-++++",
"+++++-++++" };*/
String[] sInput = new String[10];
for(int i = 0; i < sInput.length; i++) {
sInput[i] = br.readLine();
}
char[][] input = transformInput(sInput);
//printMatrix(input); //PRINT
List<StrWord>[] strWords = (List<StrWord>[]) new List[11]; //index = word length
for(int i = 0; i < strWords.length; i++) {
strWords[i] = new ArrayList<>();
}
String wordsToFill;
//wordsToFill = "LONDON;DELHI;ICELAND;ANKARA";
//wordsToFill = "SYDNEY;TURKEY;DETROIT;EGYPT;PARIS";
wordsToFill = br.readLine();
int wordCount = fillWords(strWords, wordsToFill);
//StrWord.print(strWords); //PRINT
Word[] objWords = new Word[wordCount];
markObjWords(objWords, input);
//Word.printAll(objWords); //PRINT
//check words unique in length
searchByLen(objWords, strWords);
//check words by intersections
searchByIntersection(objWords, strWords); //TODO: can invoke it twice on failure
//System.out.println();
//Word.printAll(objWords); //PRINT
//StrWord.print(strWords); //PRINT
callTheOrcs(objWords, strWords);
printResultMatrix(objWords, input);
//TODO: on failure try all the combinations that are left checking for overriden words
}
private static void callTheOrcs(Word[] objWords, List<StrWord>[] strWords) {
for(Word objW : objWords) {
if(!objW.matchFound) {
for(int i = 0; i < strWords[objW.length].size(); i++) {
StrWord strW = strWords[objW.length].get(i);
if(!strW.used) {
strW.used = true;
objW.matchFound = true;
objW.matchWord = strW.word;
}
}
}
}
}
private static void printResultMatrix(Word[] words, char[][] m) {
for(Word word : words) {
if(word.direction == Direction.HORIZONTAL) {
for(int j = 0; j < word.length; j++) {
m[word.row][word.col + j] = word.matchWord.charAt(j);
}
} else {
for(int i = 0; i < word.length; i++) {
m[word.row + i][word.col] = word.matchWord.charAt(i);
}
}
}
printMatrixNoWhitespace(m);
}
private static void searchByLen(Word[] objWords, List<StrWord>[] strWords) {
for(List<StrWord> list : strWords) {
if(list.size() == 1) {
int wordLen = list.get(0).word.length();
for(int i = 0; i < objWords.length; i++) {
if(objWords[i].length == wordLen) {
objWords[i].matchFound = true;
objWords[i].matchWord = list.get(0).word;
list.get(0).used = true;
}
}
}
}
}
private static void searchByIntersection(Word[] objWords, List<StrWord>[] strWords) {
for(Word obj : objWords) {
if(!obj.matchFound) {
//System.out.println(obj.row + " " + obj.col);
for(Intersection intr : obj.intrList) {
int matches = 0;
StrWord w1, w2, matchW1 = null, matchW2 = null;
//check current word and the intersecting word
//if only one match exists set both, the check is made only if
//current word is not used
for(int a = 0; a < strWords[obj.length].size(); a++) {
w1 = strWords[obj.length].get(a);
//if(!w1.used) {
for(int b = 0; b < strWords[intr.otherWordLen].size(); b++) {
//if both words are with same lengths
if(obj.length == intr.otherWordLen && a == b) {
continue;
}
w2 = strWords[intr.otherWordLen].get(b);
if(w1.word.charAt(intr.currCharPosition) ==
w2.word.charAt(intr.otherCharPosition)) {
matches++;
matchW1 = w1;
matchW2 = w2;
}
}
//}
}
if(matches == 1) {
matchW1.used = true;
obj.matchFound = true;
obj.matchWord = matchW1.word;
Word other = findWord(objWords, intr.otherStartRow, intr.otherStartCol,
obj.direction == Direction.HORIZONTAL ? Direction.VERTICAL : Direction.HORIZONTAL);
matchW2.used = true;
other.matchFound = true;
other.matchWord = matchW2.word;
//System.out.println("MATCH w1 : " + matchW1.word + " w2: " + matchW2.word);
}
}
}
}
}
private static Word findWord(Word[] words, int startRow, int startCol, Direction direction) {
for(Word w : words) {
if(w.row == startRow && w.col == startCol && w.direction == direction) {
return w;
}
}
return null;
}
private static char[][] transformInput(String[] input) {
int len = input.length;
char[][] m = new char[len][len];
for(int i = 0; i < len; i++) {
m[i] = input[i].toCharArray();
}
return m;
}
private static int fillWords(List<StrWord>[] words, String input) {
String[] sArr = input.split(";");
for(int i = 0; i < sArr.length; i++) {
words[sArr[i].length()].add(new StrWord(sArr[i]));
}
return sArr.length;
}
private static void printMatrix(char[][] m) {
int rows = m.length;
int cols = m[0].length;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
private static void printMatrixNoWhitespace(char[][] m) {
int rows = m.length;
int cols = m[0].length;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(m[i][j]);
}
System.out.println();
}
}
private static void markObjWords(Word[] words, char[][] m) {
int len = m.length;
int wordRow, wordCol, wordLen;
Direction wordDirection;
int wordIndex = 0;
for(int i = 0; i < len; i++) {
for(int j = 0; j < len; j++) {
if(m[i][j] == MINUS) {
//check if found vert word
if( ((i > 0 && m[i-1][j] != MINUS) || i == 0) &&
i+1 < len && m[i+1][j] == MINUS ) {
wordRow = i;
wordCol = j;
wordDirection = Direction.VERTICAL;
wordLen = 0;
List<Intersection> intrList = new ArrayList<>();
while(i < len && m[i][j] == MINUS) {
Intersection intr = getHorizIntersection(i, j , m);
if(intr != null) {
intr.currCharPosition = wordLen;
intrList.add(intr);
}
wordLen++;
i++;
}
i = wordRow;
words[wordIndex++] = new Word(wordRow, wordCol, wordLen, wordDirection, intrList);
}
//check if found horiz word
if( ((j > 0 && m[i][j-1] != MINUS) || j == 0) &&
j+1 < len && m[i][j+1] == MINUS) {
wordRow = i;
wordCol = j;
wordDirection = Direction.HORIZONTAL;
wordLen = 0;
List<Intersection> intrList = new ArrayList<>();
while(j < len && m[i][j] == MINUS) {
Intersection intr = getVertIntersection(i, j , m);
if(intr != null) {
intr.currCharPosition = wordLen;
intrList.add(intr);
}
wordLen++;
j++;
}
j = wordCol;
words[wordIndex++] = new Word(wordRow, wordCol, wordLen, wordDirection, intrList);
}
}
}
}
}
private static Intersection getHorizIntersection(int i, int j, char[][] m) {
Intersection res = null;
int len = m.length;
if( (j - 1 >= 0 && m[i][j-1] == MINUS) ||
(j + 1 < len && m[i][j+1] == MINUS) ) {
res = new Intersection();
int oldJ = j;
//move j to start of intersecting word
while(j >= 0 && m[i][j] == MINUS) {
j--;
}
j++;
res.otherStartRow = i;
res.otherStartCol = j;
while(j < len && m[i][j] == MINUS) {
if(j == oldJ) {
res.otherCharPosition = res.otherWordLen;
}
res.otherWordLen++;
j++;
}
}
return res;
}
private static Intersection getVertIntersection(int i, int j, char[][] m) {
Intersection res = null;
int len = m.length;
if( (i - 1 >= 0 && m[i-1][j] == MINUS) ||
(i + 1 < len && m[i+1][j] == MINUS) ) {
res = new Intersection();
int oldI = i;
//move i to start of intersecting word
while(i >= 0 && m[i][j] == MINUS) {
i--;
}
i++;
res.otherStartRow = i;
res.otherStartCol = j;
while(i < len && m[i][j] == MINUS) {
if(i == oldI) {
res.otherCharPosition = res.otherWordLen;
}
res.otherWordLen++;
i++;
}
}
return res;
}
}
class Intersection {
//direction is opposite of current word
public int currCharPosition;
public int otherWordLen;
public int otherCharPosition;
public int otherStartRow;
public int otherStartCol;
@Override
public String toString() {
return String.format(" -> otherStartRow = %d, otherStartCol = %d, "+
"otherWordLen = %d, otherCharPosition = %d, currCharPosition = %d",
otherStartRow, otherStartCol, otherWordLen, otherCharPosition , currCharPosition);
}
}
class StrWord {
public String word;
public boolean used;
public StrWord(String word) {
this.word = word;
this.used = false;
}
@Override
public String toString() {
return word + " used: " + used;
}
public static void print(List<StrWord>[] strWords) {
for(int i = 0; i < strWords.length; i++) {
System.out.println(i + " " + strWords[i]);
}
}
}
class Word {
public final int row;
public final int col;
public final int length;
public final Direction direction;
public boolean matchFound = false;
public String matchWord;
List<Intersection> intrList;
public Word(int row, int col, int length, Direction direction, List<Intersection> intrList) {
this.row = row;
this.col = col;
this.length = length;
this.direction = direction;
this.intrList = intrList;;
}
@Override
public String toString() {
return String.format("row = %d, col = %d, length = %d, direction = %s "+
"matchFound = %b, matchWord = %s%n"+
"Intersections: %s", row, col, length, direction, matchFound, matchWord, intrList);
}
public static void printAll(Word... words) {
for(Word item : words) {
System.out.println(item);
}
}
}
enum Direction {
HORIZONTAL, VERTICAL;
}