-
Notifications
You must be signed in to change notification settings - Fork 77
/
CrosswordProblemBacktracking.cpp
213 lines (196 loc) · 4.54 KB
/
CrosswordProblemBacktracking.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// A crossword of 10*10 grid. The grid contains '+' or '-' as its cell values. Now, you are also provided with a word list that needs to placed accurately in the grid. Cells marked with '-' are to be filled with word list.
// Input format:
// The first 10 lines of input contain crossword. Each of 10 lines has a character array of size 10. Input characters are either '+' or '-'.
// The next line of input contains the word list, in which each word is separated by ';'.
// Output format:
// Print the crossword grid, after placing the words of word list in '-' cells.
// Constraints
// The number of words in the word list lie in the range of: [1,6]
// The length of words in the word list lie in the range: [1, 10]
// Input :
// +-++++++++
// +-++-+++++
// +-------++
// +-++-+++++
// +-++-+++++
// +-++-+++++
// ++++-+++++
// ++++-+++++
// ++++++++++
// ----------
// CALIFORNIA;NIGERIA;CANADA;TELAVIV
// Output :
// +C++++++++
// +A++T+++++
// +NIGERIA++
// +A++L+++++
// +D++A+++++
// +A++V+++++
// ++++I+++++
// ++++V+++++
// ++++++++++
// CALIFORNIA
#include <iostream>
using namespace std;
char crossword[10][10];
void resetVertical(int row, int col, bool state[], int size)
{
int j = row;
for(int i=0; i<size; i++)
{
if(state[i] == true)
{
crossword[j][col] = '-';
state[i] = false;
}
j++;
}
}
void setVertical(int row, int col, string name, bool state[])
{
int j = row;
for(int i=0; i<name.size(); i++)
{
if(crossword[j][col] == '-')
{
crossword[j][col] = name[i];
state[i] = true;
}
else
{
state[i] = false;
}
j++;
}
}
bool isValidVertical(int row, int col, string name)
{
if(name.size() + row > 10)
{
return false;
}
int j = row;
for(int i=0; i<name.size(); i++)
{
if(crossword[j][col] != '-' && crossword[j][col] != name[i])
{
return false;
}
j++;
}
return true;
}
void resetHorizontal(int row, int col, bool state[], int size)
{
int j = col;
for(int i=0; i<size; i++)
{
if(state[i] == true)
{
crossword[row][j] = '-';
state[i] = false;
}
j++;
}
}
void setHorizontal(int row, int col, string name, bool state[])
{
int j = col;
for(int i=0; i<name.size(); i++)
{
if(crossword[row][j] == '-')
{
crossword[row][j] = name[i];
state[i] = true;
}
else
{
state[i] = false;
}
j++;
}
}
bool isValidHorizontal(int row, int col, string name)
{
if(name.size() + col > 10)
{
return false;
}
int j = col;
for(int i=0; i<name.size(); i++)
{
if(crossword[row][j] != '-' && crossword[row][j] != name[i])
{
return false;
}
j++;
}
return true;
}
void solveCrossword(string names[], int l, int index)
{
if(index == l)
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<crossword[i][j];
}
cout<<endl;
}
return;
}
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(crossword[i][j] == '-' || crossword[i][j] == names[index][0])
{
int size = names[index].size();
bool state[size];
if(isValidHorizontal(i, j, names[index]))
{
setHorizontal(i, j, names[index], state);
solveCrossword(names, l, index + 1);
resetHorizontal(i, j, state, size);
}
if(isValidVertical(i, j, names[index]))
{
setVertical(i, j, names[index], state);
solveCrossword(names, l, index + 1);
resetVertical(i, j, state, size);
}
}
}
}
}
int main()
{
string str;
for(int i=0; i<10; i++)
{
cin>>str;
for(int j=0; j<10; j++)
{
crossword[i][j] = str[j];
}
}
string name, word="", names[6];
cin>>name;
int l = 0;
for(int i=0; i<name.length(); i++)
{
if(name[i] == ';')
{
names[l++] = word;
word = "";
}
else
{
word += name[i];
}
}
names[l++] = word;
solveCrossword(names, l, 0);
}