-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
158 lines (123 loc) · 3.93 KB
/
solution.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
#include<bits/stdc++.h>
using namespace std;
// checks all positions in the grid where it's possible to place the strings
bool placeString(vector<string>& grid, const string& l) {
int len = l.length();
bool placed = false;
// check the horizontal positions
// checking each row
for (int i = 0; i < 10; i++) {
if (placed)
break;
else
placed = true;
// checking each part of the row
for (int j = 0; j <= (10 - len); j++) {
placed = true;
for (int k = j; k < (len + j); k++) {
// wasn't able to be placed here, break
if (grid[i][k] == '+') {
placed = false;
break;
} else if (grid[i][k] == '-') {
// Continue
} else if (l[k - j] != grid[i][k]) {
// wasn't able to be placed here, break
placed = false;
break;
}
}
// if placed, update grid nd break
if (placed) {
for (int k = j; k < (len + j); k++)
grid[i][k] = l[k - j];
break;
}
}
}
// check the vertical positions
if (!placed) {
// now check each column
for (int i = 0; i < 10; i++) {
if (placed)
break;
else
placed = true;
// check each position in the column
for (int j = 0; j <= (10 - len); j++) {
placed = true;
for (int k = j; k < (len + j); k++) {
if (grid[k][i] == '+') {
placed = false;
break;
} else if (grid[k][i] == '-') {
} else if (l[k - j] != grid[k][i]) {
placed = false;
break;
}
}
if (placed) {
for (int k = j; k < (len + j); k++)
grid[k][i] = l[k - j];
break;
}
}
}
}
return placed;
}
// tries to place the strings in the given order
vector<string> finishGrid(vector<string> grid, vector<string> placesNames) {
vector<string> completedGrid;
int len = placesNames.size();
bool success = true;
for (int i = 0; i < len; i++) {
string l = placesNames[i];
if (!placeString(grid, l)) {
success = false;
completedGrid.push_back("nope");
break;
}
}
if (success)
completedGrid = grid;
return completedGrid;
}
// Driver Code
int main() {
// Input of grid and String of words
vector<string> grid;
string s;
for (int i = 0; i < 10; i++) {
cin >> s;
grid.push_back(s);
}
cin >> s;
vector<string> placesNames;
int len = s.length();
int start = 0;
// find ; as words are seperated by ; only
// initialing word to temp
for (int i = 0; i < len; i++) {
if (s[i] == ';') {
string temp(s.begin() + start, s.begin() + i);
placesNames.push_back(temp);
start = i + 1;
}
}
string temp(s.begin() + start, s.end());
placesNames.push_back(temp);
// sorting the letters of word
sort(placesNames.begin(), placesNames.end());
vector<string> finishedGrid;
// run loop for all permutations
do {
finishedGrid = finishGrid(grid, placesNames);
if (finishedGrid[0] != "nope")
break;
} while (next_permutation(placesNames.begin(), placesNames.end()));
// Display output
for (int i = 0; i < 10; i++)
cout << finishedGrid[i] << endl;
return 0;
}