-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCrossWord
More file actions
179 lines (176 loc) · 3.75 KB
/
CrossWord
File metadata and controls
179 lines (176 loc) · 3.75 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
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
code in C++
**********************************************
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define n 10
void printer(char cross[n][n])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << cross[i][j];
}
cout << endl;
}
cout << endl;
}
bool isvalid_vertical(char cross[n][n], int r, int c, string word)
{
int j = r;
for (int i = 0;i < word.length();i++)
{
if (j > 9)
return false;
if (cross[j][c] == '+' || (cross[j][c] != '-' && cross[j][c] != word[i]))
return false;
if (cross[j][c] == '-' || cross[j][c] == word[i])
j++;
}
return true;
}
bool isvalid_horizontal(char cross[n][n], int r, int c, string word)
{
int j = c;
for (int i = 0;i < word.length();i++) {
if (j > 9)
return false;
if (cross[r][j] == '+' || (cross[r][j] != '-' && cross[r][j] != word[i]))
return false;
if (cross[r][j] == '-' || cross[r][j] == word[i])
j++;
}
return true;
}
void set_vertical(char cross[n][n], int r, int c, vector<bool>& v, string word)
{
int row = r;
for (int i = 0; i < word.length(); i++)
{
if (cross[row + i][c] == '-')
{
cross[row + i][c] = word[i];
v.push_back(true);
}
else
{
v.push_back(false);
}
}
}
void reset_vertical(char cross[n][n], int r, int c, vector<bool>v)
{
int row = r;
for (int i = 0; i < v.size(); i++)
{
if (v[i])
{
cross[row + i][c] = '-';
}
}
}
void set_horizontal(char cross[n][n], int r, int c, vector<bool>& v, string word)
{
int col = c;
for (int i = 0; i < word.length(); i++)
{
if (cross[r][col + i] == '-')
{
cross[r][col + i] = word[i];
v.push_back(true);
}
else
{
v.push_back(false);
}
}
}
void reset_horizontal(char cross[n][n], int r, int c, vector<bool>v)
{
int col = c;
for (int i = 0; i < v.size(); i++)
{
if (v[i])
{
cross[r][col + i] = '-';
}
}
}
bool crossword(char cross[n][n], vector<string>words, int index)
{
//base case is if index become == v.size()----> then print the crossword;
if (index >= words.size())
{
printer(cross);
return true;
}
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++)
{
if (cross[r][c] == '-' || cross[r][c] == words[index][0])
{
if (isvalid_vertical(cross, r, c, words[index]))
{
//maintain a helper array
vector<bool>helper;
set_vertical(cross, r, c, helper, words[index]);
//pass this helper array in the function set_vertical where this helper array is a boolean array.
//set vertical(helper, ...., ....)
if (crossword(cross, words, index + 1))
{
return true;
}
//setvertical call karne k baad crossword call karna hai for next index words.
// crossword(cross, words, index+1)
// ye jo crossword function hai wo boolean hai. if it returns true, just return.
// or if it return false, call the reset vertical function---> reset_vertical(helper, ....)
reset_vertical(cross, r, c, helper);
// agar is valid vertical false return karta hai to hum isvalid_horizontal mei check kar lenge.
// isme bhi vhi hoga helper, set_horizontal, crossword, reset_horizontal.
}
if (isvalid_horizontal(cross, r, c, words[index]))
{
vector<bool>helper;
set_horizontal(cross, r, c, helper, words[index]);
if (crossword(cross, words, index + 1))
{
return true;
}
reset_horizontal(cross, r, c, helper);
}
}
}
}
return false;
}
int main()
{
char arr[n][n];
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int j = 0; j < n; j++)
{
arr[i][j] = s[j];
}
}
string s;
cin >> s;
vector<string>words;
for (int i = 0; i < s.length(); i++)
{
int j = i;
while (s[j] != ';' && j < s.length())
{
j++;
}
words.push_back(s.substr(i, j - i));
i = j;
j++;
}
bool x = crossword(arr, words, 0);
}