Skip to content

Commit 831c36c

Browse files
authored
Merge pull request AllAlgorithms#211 from AllAlgorithms/backtracking
Create crossword_puzzle.cpp
2 parents 4875c62 + 3d47f0a commit 831c36c

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

backtracking/crossword_puzzle.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// ways are to calculate the number of
5+
// possible ways to fill the grid
6+
int ways = 0;
7+
8+
// this function is used to print
9+
// the resultant matrix
10+
void printMatrix(vector<string>& matrix, int n)
11+
{
12+
for (int i = 0; i < n; i++)
13+
cout << matrix[i] << endl;
14+
}
15+
16+
// this function checks for the current word
17+
// if it can be placed horizontally or not
18+
// x -> it represent index of row
19+
// y -> it represent index of column
20+
// currentWord -> it represent the
21+
// current word in word array
22+
vector<string> checkHorizontal(int x, int y,
23+
vector<string> matrix,
24+
string currentWord)
25+
{
26+
int n = currentWord.length();
27+
28+
for (int i = 0; i < n; i++) {
29+
if (matrix[x][y + i] == '#' ||
30+
matrix[x][y + i] == currentWord[i]) {
31+
matrix[x][y + i] = currentWord[i];
32+
}
33+
else {
34+
35+
// this shows that word cannot
36+
// be placed horizontally
37+
matrix[0][0] = '@';
38+
return matrix;
39+
}
40+
}
41+
42+
return matrix;
43+
}
44+
45+
// this function checks for the current word
46+
// if it can be placed vertically or not
47+
// x -> it represent index of row
48+
// y -> it represent index of column
49+
// currentWord -> it represent the
50+
// current word in word array
51+
vector<string> checkVertical(int x, int y,
52+
vector<string> matrix,
53+
string currentWord)
54+
{
55+
int n = currentWord.length();
56+
57+
for (int i = 0; i < n; i++) {
58+
if (matrix[x + i][y] == '#' ||
59+
matrix[x + i][y] == currentWord[i]) {
60+
matrix[x + i][y] = currentWord[i];
61+
}
62+
else {
63+
64+
// this shows that word
65+
// cannot be placed vertically
66+
matrix[0][0] = '@';
67+
return matrix;
68+
}
69+
}
70+
return matrix;
71+
}
72+
73+
// this function recursively checks for every
74+
// word that can align vertically in one loop
75+
// and in another loop it checks for those words
76+
// that can align horizontally words -> it
77+
// contains all the words to fill in a crossword
78+
// puzzle matrix -> it contain the current
79+
// state of crossword index -> it represent
80+
// the index of current word n -> it represent
81+
// the length of row or column of the square matrix
82+
void solvePuzzle(vector<string>& words,
83+
vector<string> matrix,
84+
int index, int n)
85+
{
86+
if (index < words.size()) {
87+
string currentWord = words[index];
88+
int maxLen = n - currentWord.length();
89+
90+
// loop to check the words that can align vertically.
91+
for (int i = 0; i < n; i++) {
92+
for (int j = 0; j <= maxLen; j++) {
93+
vector<string> temp = checkVertical(j, i,
94+
matrix, currentWord);
95+
96+
if (temp[0][0] != '@') {
97+
solvePuzzle(words, temp, index + 1, n);
98+
}
99+
}
100+
}
101+
102+
// loop to check the words that can align horizontally.
103+
for (int i = 0; i < n; i++) {
104+
for (int j = 0; j <= maxLen; j++) {
105+
vector<string> temp = checkHorizontal(i, j,
106+
matrix, currentWord);
107+
108+
if (temp[0][0] != '@') {
109+
solvePuzzle(words, temp, index + 1, n);
110+
}
111+
}
112+
}
113+
}
114+
else {
115+
// calling of print function to
116+
// print the crossword puzzle
117+
cout << (ways + 1) << " way to solve the puzzle "
118+
<< endl;
119+
printMatrix(matrix, n);
120+
cout << endl;
121+
122+
// increase the ways
123+
ways++;
124+
return;
125+
}
126+
}
127+
128+
// Driver Code
129+
int main()
130+
{
131+
// length of grid
132+
int n1 = 10;
133+
134+
// matrix to hold the grid of puzzle
135+
vector<string> matrix;
136+
137+
// take input of puzzle in matrix
138+
// input of grid of size n1 x n1
139+
matrix.push_back("*#********");
140+
matrix.push_back("*#********");
141+
matrix.push_back("*#****#***");
142+
matrix.push_back("*##***##**");
143+
matrix.push_back("*#****#***");
144+
matrix.push_back("*#****#***");
145+
matrix.push_back("*#****#***");
146+
matrix.push_back("*#*######*");
147+
matrix.push_back("*#********");
148+
matrix.push_back("***#######");
149+
150+
vector<string> words;
151+
152+
// the words matrix will hold all
153+
// the words need to be filled in the grid
154+
words.push_back("PUNJAB");
155+
words.push_back("JHARKHAND");
156+
words.push_back("MIZORAM");
157+
words.push_back("MUMBAI");
158+
159+
// initialize the number of ways
160+
// to solve the puzzle to zero
161+
ways = 0;
162+
163+
// recursive function to solve the puzzle
164+
// Here 0 is the initial index of words array
165+
// n1 is length of grid
166+
solvePuzzle(words, matrix, 0, n1);
167+
cout << "Number of ways to fill the grid is "
168+
<< ways << endl;
169+
170+
return 0;
171+
}

0 commit comments

Comments
 (0)