-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
170 lines (164 loc) · 5.55 KB
/
solution.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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution
{
public static void print(String crossword[])
{
for(int i=0;i<crossword.length;i++)
{
for(int j=0;j<crossword[0].length();j++)
{
System.out.print(crossword[i].charAt(j));
}
System.out.println();
}
}
// Complete the crosswordPuzzle function below.
public static void solution(String crossword[],String word[],int c)
{
if(c==word.length)
{
print(crossword);
return;
}
for(int i=0;i<crossword.length;i++)
{
for(int j=0;j<crossword[0].length();j++)
{
if(crossword[i].charAt(j)=='-' || crossword[i].charAt(j)==word[c].charAt(0))
{
if(CanBePlacedHorizontally(crossword,i,j,word[c]))
{
//System.out.println("1");
boolean arr[]=placehorizontally(crossword,i,j,word[c]);
solution(crossword,word,c+1);
unplacehorizontally(crossword,i,j,arr);
}
if(CanBePlacedVertically(crossword,i,j,word[c]))
{
//System.out.println("2");
boolean ar[]=placevertically(crossword,i,j,word[c]);
solution(crossword,word,c+1);
unplacevertically(crossword,i,j,ar);
}
}
}
}
}
public static boolean CanBePlacedHorizontally(String []crossword,int i,int j,String word)
{
int k=word.length();
if(j+k>10)
return false;
if(j-1>=0 && (crossword[i].charAt(j-1)!='+'&&crossword[i].charAt(j-1)!='X'))
return false;
if(j+k<10 && (crossword[i].charAt(j+k)!='+'&&crossword[i].charAt(j+k)!='X'))
return false;
for(int p=0;p<word.length();p++)
{
if(crossword[i].charAt(j+p)=='-' || crossword[i].charAt(j+p)==word.charAt(p))
continue;
return false;
}
return true;
}
public static boolean[] placehorizontally(String []crossword,int i,int j,String word)
{
String myName = crossword[i];
boolean arr[]=new boolean[word.length()];
char[] myNameChars = myName.toCharArray();
for(int k=0;k<word.length();k++)
{
if(crossword[i].charAt(j+k)=='-')
{
myNameChars[j+k] = word.charAt(k);
arr[k]=true;
}
}
myName = String.valueOf(myNameChars);
crossword[i]=myName;
return arr;
}
public static void unplacehorizontally(String []crossword,int i,int j,boolean arr[])
{
String myName = crossword[i];
char[] myNameChars = myName.toCharArray();
for(int k=0;k<arr.length;k++)
{
if(arr[k]==true)
myNameChars[j+k] = '-';
}
myName = String.valueOf(myNameChars);
crossword[i]=myName;
}
public static boolean CanBePlacedVertically(String []crossword,int i,int j,String word)
{
int k=word.length();
if(i+k>10)
return false;
if(i-1>=0 && (crossword[i-1].charAt(j)!='+'&&crossword[i-1].charAt(j)!='X'))
return false;
if(i+k<10 && (crossword[i+k].charAt(j)!='+'&&crossword[i+k].charAt(j)!='X'))
return false;
for(int p=0;p<word.length();p++)
{
if(crossword[i+p].charAt(j)=='-' || crossword[i+p].charAt(j)==word.charAt(p))
continue;
return false;
}
return true;
}
public static boolean[] placevertically(String []crossword,int i,int j,String word)
{
boolean ar[]=new boolean[word.length()];
for(int k=0;k<word.length();k++)
{
String myName = crossword[i+k];
char[] myNameChars = myName.toCharArray();
if(crossword[i+k].charAt(j)=='-')
{
ar[k]=true;
myNameChars[j] = word.charAt(k);
}
myName = String.valueOf(myNameChars);
crossword[i+k]=myName;
}
return ar;
}
public static void unplacevertically(String []crossword,int i,int j,boolean ar[])
{
for(int k=0;k<ar.length;k++)
{
String myName = crossword[i+k];
char[] myNameChars = myName.toCharArray();
if(ar[k])
myNameChars[j] = '-';
myName = String.valueOf(myNameChars);
crossword[i+k]=myName;
}
}
static String[] crosswordPuzzle(String[] crossword, String words)
{
String word[]=words.split(";",-1);
//making an array of words
solution(crossword,word,0);
return crossword;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
String[] crossword = new String[10];
for (int i = 0; i < 10; i++) {
String crosswordItem = scanner.nextLine();
crossword[i] = crosswordItem;
}
String words = scanner.nextLine();
String[] result = crosswordPuzzle(crossword, words);
scanner.close();
}
}