forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossWord.java
95 lines (93 loc) · 3.3 KB
/
CrossWord.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
import java.io.*;
import java.util.*;
public class Main {
static char[][] arr = new char[10][10];
static String[] words;
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
arr[i][j] = scn.nextChar();
int n = scn.nextInt();
words = new String[n];
for (int i = 0; i < n; i++)
words[i] = scn.next();
solution(0);
}
static void solution(int vidx) {
if (vidx == words.length()) {
print();
return;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (canPlaceHorizontally(i, j, words[vidx])) {
placeHorizontally(i, j, words[vidx]);
solution(vidx + 1);
removeHorizontally(i, j, words[vidx]);
}
if (canPlaceVertically(i, j, words[vidx])) {
placeVertically(i, j, words[vidx]);
solution(vidx + 1);
removeVertically(i, j, words[vidx]);
}
}
}
}
static boolean canPlaceHorizontally(int i, int j, String word) {
int n = word.length();
if (i < 0 || j < 0 || i == 10 || j == 10 || j + n - 1 >= 10)
return false;
if (j - 1 >= 0 && arr[i][j - 1] != '+')
return false;
if (j + n < 10 && arr[i][j + n] != '+')
return false;
for (int c = 0; c < n; c++)
if (!(arr[i][j + c] == '-' || arr[i][j + c] == word.charAt(c)))
return false;
return true;
}
static void placeHorizontally(int i, int j, String word) {
int n = word.length();
for (int c = 0; c < n; c++)
arr[i][j + c] = word.charAt(c);
}
static void removeHorizontally(int i, int j, String word) {
int n = word.length();
for (int c = 0; c < n; c++)
if ((i - 1 < 0 || arr[i - 1][j + c] == '+') && (i + 1 >= 10 || arr[i + 1][j + c] == '+'))
arr[i][j + c] = '-';
}
static boolean canPlaceVertically(int i, int j, String word) {
int n = word.length();
if (i < 0 || j < 0 || i == 10 || j == 10 || i + n - 1 >= 10)
return false;
if (i - 1 >= 0 && arr[i - 1][j] != '+')
return false;
if (i + n < 10 && arr[i + n][j] != '+')
return false;
for (int c = 0; c < n; c++)
if (!(arr[i + c][j] == '-' || arr[i + c][j] == word.charAt(c)))
return false;
return true;
}
static void placeVertically(int i, int j, String word) {
int n = word.length();
for (int c = 0; c < n; c++)
arr[i + c][j] = word.charAt(c);
}
static void removeVertically(int i, int j, String word) {
int n = word.length();
for (int c = 0; c < n; c++)
if ((j - 1 < 0 || arr[i + c][j - 1] == '+') && (j + 1 >= 10 || arr[i + c][j + 1] == '+'))
arr[i + c][j] = '-';
}
static void print() {
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();
}
System.out.println();
}
}