-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossword Puzzle
More file actions
153 lines (121 loc) · 3.19 KB
/
Crossword Puzzle
File metadata and controls
153 lines (121 loc) · 3.19 KB
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static void print(char[][] board) {
for (int r = 0; r < 10; r++) {
System.out.println(new String(board[r]));
}
//System.out.println();
}
private static boolean solve(char[][] board, List<String> words, List<Integer[]> h, List<Integer[]> v, Set<String> used) {
if (used.size() == words.size()) {
print(board);
return true;
}
for (String w : words) {
if (!used.contains(w)) {
// HORIZONTAL
for (Integer[] pos : h) {
int row = pos[0];
int col = pos[1];
//System.out.print("Tesing position "+row+","+col+" for word "+w+"...");
boolean fit = true;
// Backup
char[] backup = Arrays.copyOf(board[row], 10);
// Place the word
for (int i = col; i < col + w.length(); i++) {
if (i >= 10 || board[row][i] == '+' || (board[row][i] != '-' && board[row][i] != w.charAt(i - col))) {
fit = false;
break;
}
board[row][i] = w.charAt(i - col);
}
//System.out.println("fit="+fit);
// Recursive call
if (fit) {
used.add(w);
boolean found = solve(board, words, h, v, used);
if (found) {
return true;
}
used.remove(w);
}
// Restore
board[row] = Arrays.copyOf(backup, 10);
}
// VERTICAL
for (Integer[] pos : v) {
int row = pos[0];
int col = pos[1];
boolean fit = true;
// Backup
char[] backup = new char[10];
for (int r = 0; r < 10; r++) {
backup[r] = board[r][col];
}
// Place the word
for (int i = row; i < row + w.length(); i++) {
if (i >= 10 || board[i][col] == '+' || (board[i][col] != '-' && board[i][col] != w.charAt(i - row))) {
fit = false;
break;
}
board[i][col] = w.charAt(i - row);
}
//System.out.println("fit="+fit);
// Recursive call
if (fit) {
used.add(w);
boolean found = solve(board, words, h, v, used);
if (found) {
return true;
}
used.remove(w);
}
// Restore
for (int r = 0; r < 10; r++) {
board[r][col] = backup[r];
}
}
}
}
return false;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
char[][] board = new char[10][10];
for (int r = 0; r < 10; r++) {
board[r] = in.next().toCharArray();
}
String[] w = in.next().split(";");
List<String> words = new ArrayList<String>();
for (String word : w) {
words.add(word);
}
List<Integer[]> h = new ArrayList<Integer[]>();
List<Integer[]> v = new ArrayList<Integer[]>();
for (int r = 0; r < 10; r++) {
for (int c = 0; c < 10; c++) {
if (board[r][c] == '-') {
// HORIZONTAL
// Go all the way to the left
int start = c;
while (start > 0 && board[r][start - 1] == '-') {
start--;
}
h.add(new Integer[] { r, start });
// VERTICAL
// Go all the way up
start = r;
while (start > 0 && board[start - 1][c] == '-') {
start--;
}
v.add(new Integer[] { start, c });
}
}
}
solve(board, words, h, v, new HashSet<String>());
}
}