-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCrossWordPuzzle.java
132 lines (104 loc) · 3.72 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
import java.io.*;
import java.util.*;
public class Main {
// Code
// Level -> Parameter
// Calls -> Loop
public static void solution(char[][] arr, String[] words, int vidx) {
if(vidx == words.length) {
print(arr);
return;
}
String word = words[vidx];
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[0].length; j++) {
if(canPlaceWordHorizontally(arr, word, i, j)) {
boolean wePlaced[] = placeHorizontally(arr, word, i, j);
solution(arr, words, vidx+1);
unplaceHorizontally(arr, wePlaced, i, j);
}
if(canPlaceWordVertically(arr, word, i, j)) {
boolean wePlaced[] = placeVertically(arr, word, i, j);
solution(arr, words, vidx+1);
unplaceVertically(arr, wePlaced, i, j);
}
}
}
}
public static boolean canPlaceWordHorizontally(char arr[][], String word, int i, int j) {
if(j-1>=0 && arr[i][j-1] != '+') return false;
else if(j+word.length() < arr[0].length && arr[i][j+word.length()] != '+') return false;
for(int jj=0; jj<word.length(); jj++) {
if(j+jj >= arr[0].length) return false;
if(arr[i][j+jj] == '-' || arr[i][j+jj] == word.charAt(jj)) continue;
else return false;
}
return true;
}
public static boolean canPlaceWordVertically(char arr[][], String word, int i, int j) {
if(i-1>=0 && arr[i-1][j] != '+') return false;
else if(i+word.length() < arr.length && arr[i+word.length()][j] != '+') return false;
for(int ii=0; ii<word.length(); ii++) {
if(i+ii >= arr.length) return false;
if(arr[i+ii][j] == '-' || arr[i+ii][j] == word.charAt(ii)) continue;
else return false;
}
return true;
}
public static boolean[] placeHorizontally(char arr[][], String word, int i, int j) {
boolean wePlaced[] = new boolean[word.length()];
for(int jj=0; jj<word.length(); jj++) {
if(arr[i][j+jj] == '-') {
arr[i][j+jj] = word.charAt(jj);
wePlaced[jj] = true;
}
}
return wePlaced;
}
public static boolean[] placeVertically(char arr[][], String word, int i, int j) {
boolean wePlaced[] = new boolean[word.length()];
for(int ii=0; ii<word.length(); ii++) {
if(arr[i+ii][j] == '-') {
arr[i+ii][j] = word.charAt(ii);
wePlaced[ii] = true;
}
}
return wePlaced;
}
public static void unplaceHorizontally(char arr[][], boolean wePlaced[], int i, int j) {
for(int jj=0; jj<wePlaced.length; jj++) {
if(wePlaced[jj] == true) {
arr[i][j+jj] = '-';
}
}
}
public static void unplaceVertically(char arr[][], boolean wePlaced[], int i, int j) {
for(int ii=0; ii<wePlaced.length; ii++) {
if(wePlaced[ii] == true) {
arr[i+ii][j] = '-';
}
}
}
public static void print(char[][] arr) {
for (int i = 0 ; i < arr.length; i++) {
for (int j = 0 ; j < arr.length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
char[][] arr = new char[10][10];
for (int i = 0 ; i < arr.length; i++) {
String str = scn.next();
arr[i] = str.toCharArray();
}
int n = scn.nextInt();
String[] words = new String[n];
for (int i = 0 ; i < words.length; i++) {
words[i] = scn.next();
}
solution(arr, words, 0);
}
}