forked from AllAlgorithms/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrosswordPuzzle.java
More file actions
148 lines (127 loc) · 3.79 KB
/
CrosswordPuzzle.java
File metadata and controls
148 lines (127 loc) · 3.79 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
import java.util.*;
public class CrosswordPuzzle {
public static void display(String[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String[][] grid = new String[10][10];
for (int i = 0; i < 10; i++) {
grid[i] = scn.nextLine().split("");
}
String[] words = scn.nextLine().split(";");
for (int i = 0; i < words.length; i++) {
if (puzzleSolved(grid, words, words[i], 0, 0)) {
break;
}
}
scn.close();
display(grid);
}
public static boolean puzzleSolved(String[][] grid, String[] words, String word, int row, int col) {
if (col >= grid.length) {
col = 0;
row += 1;
}
while (row < grid.length && !grid[row][col].equals("-")) {
col += 1;
if (col >= grid.length) {
col = 0;
row += 1;
}
}
if (row == grid.length) {
return false;
}
boolean isPlacedHoz = false, isPlacedVer = false;
while (row - 1 >= 0 && !grid[row - 1][col].equals("+")) {
row -= 1;
}
isPlacedVer = placeVertical(grid, row, col, word);
if (!isPlacedVer) {
while (col - 1 >= 0 && !grid[row][col - 1].equals("+")) {
col -= 1;
}
isPlacedHoz = placeHorizontal(grid, row, col, word);
}
if (isPlacedVer || isPlacedHoz) {
String[] remainingWords = new String[words.length - 1];
if (remainingWords.length == 0) {
return true;
}
int i = 0, j = 0;
while (i < words.length) {
if (!words[i].equals(word)) {
remainingWords[j] = words[i];
j++;
}
i++;
}
String pword = word;
for (int k = 0; k < remainingWords.length; k++) {
word = remainingWords[k];
if (puzzleSolved(grid, remainingWords, word, row, col + 1)) {
return true;
}
}
if (isPlacedHoz) {
clearHorizontal(grid, row, col, pword);
}
if (isPlacedVer) {
clearVertical(grid, row, col, pword);
}
}
return false;
}
public static boolean placeHorizontal(String[][] grid, int row, int col, String word) {
if (word.length() == 0 && (col == grid.length || grid[row][col].equals("+"))) {
return true;
} else if (col == grid.length || word.length() == 0) {
return false;
}
if (grid[row][col].equals("-") || grid[row][col].equals(word.charAt(0) + "")) {
String prev = grid[row][col];
grid[row][col] = word.charAt(0) + "";
if (placeHorizontal(grid, row, col + 1, word.substring(1))) {
return true;
}
grid[row][col] = prev;
}
return false;
}
public static boolean placeVertical(String[][] grid, int row, int col, String word) {
if (word.length() == 0 && (row == grid.length || grid[row][col].equals("+"))) {
return true;
} else if (row == grid.length || word.length() == 0) {
return false;
}
if (grid[row][col].equals("-") || grid[row][col].equals(word.charAt(0) + "")) {
String prev = grid[row][col];
grid[row][col] = word.charAt(0) + "";
if (placeVertical(grid, row + 1, col, word.substring(1))) {
return true;
}
grid[row][col] = prev;
}
return false;
}
public static void clearHorizontal(String[][] grid, int row, int col, String word) {
int pcol = col;
while (pcol < grid.length && pcol < col + word.length()) {
grid[row][pcol] = "-";
pcol++;
}
}
public static void clearVertical(String[][] grid, int row, int col, String word) {
int prow = row;
while (prow < grid.length && prow < row + word.length()) {
grid[prow][col] = "-";
prow++;
}
}
}