-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.cpp
489 lines (372 loc) · 16.5 KB
/
draft.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
#include <set>
void parsefile(std::vector<std::string>& appear,std::vector<std::string>& dont_appear,std::ifstream& puzzle, int& height, int& width){
puzzle >> width >> height;
std::string operation;
while (puzzle >> operation) {
if (operation == "+") {
// Read and store the word
std::string word;
puzzle >> word;
appear.push_back(word);
} else if (operation == "-") {
// Read and discard the word
std::string word;
puzzle >> word;
dont_appear.push_back(word);
}
}
}
void displayBoard(std::vector<std::vector<char>>& board){
for(int y = 0; y < board.size(); y++){
for (int x = 0; x < board[0].size(); x++){
std::cout << board[y][x] << " ";
}
std::cout << '\n';
}
}
bool horizontal(std::string word,std::vector<std::vector<char>>&board,int x,int y){
// checks if the current x position + the word lenghth is less than the row
if (x+word.length() > board[0].size()){
return false;
}
std::vector<std::vector<char>> board1 = board;
for (int i = 0; i < word.length(); i++){
//if it is not equal to '.' and is not equal to a letter of the word if there is a letter at anypoint it will return false
if (board1[y][x+i] != '.' && board1[y][x + i] != word[i]){
return false;
}
}
for (int i = 0; i < word.length(); i++){
// if it still hasnt returned false then you can safely add the words
board1[y][x+i] = word[i];
}
// Assign the modified board back to the original board
board = board1;
return true;
}
// same logic as the horizontal but backwards
bool horizontal_backward(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
//current x position minus word length should be less than -1 since is at position 2 in the graph and word length is 3
// 2 - 3 = -1 but it can fit so -1 < -1 is false and thus will not return false
if (x - word.length() < -1) {
return false; // Word too long to fit horizontally backward
}
for (int i = 0; i < word.length(); i++) {
if (board[y][x - i] != '.' && board[y][x - i] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y][x - i] = word[i];
}
return true;
}
//same logic as the other direction but different axis
bool vertical(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (y + word.length() > board.size()) {
return false; // Word too long to fit vertically
}
for (int i = 0; i < word.length(); i++) {
if (board[y + i][x] != '.' && board[y + i][x] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y + i][x] = word[i];
}
return true;
}
bool vertical_backward(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (y - word.length() < -1) {
return false; // Word too long to fit vertically backward
}
for (int i = 0; i < word.length(); i++) {
if (board[y - i][x] != '.' && board[y - i][x] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y - i][x] = word[i];
}
return true;
}
bool diagonal_top_left_corner(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (x + word.length() > board[0].size() || y + word.length() > board.size()) {
return false; // Word too long to fit diagonally
}
for (int i = 0; i < word.length(); i++) {
if (board[y + i][x + i] != '.' && board[y + i][x + i] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y + i][x + i] = word[i];
}
return true;
}
bool diagonal_top_left_corner_backward(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (x - word.length() < -1 || y - word.length() < -1) {
return false; // Word too long to fit diagonally backward
}
for (int i = 0; i < word.length(); i++) {
if (board[y - i][x - i] != '.' && board[y - i][x - i] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y - i][x - i] = word[i];
}
return true;
}
bool diagonal_top_right_corner(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (x - word.length() < -1 || y + word.length() > board.size()) {
return false; // Word too long to fit diagonally from top-right corner
}
for (int i = 0; i < word.length(); i++) {
if (board[y + i][x - i] != '.' && board[y + i][x - i] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y + i][x - i] = word[i];
}
return true;
}
bool diagonal_top_right_corner_backward(const std::string& word, std::vector<std::vector<char>>& board, int x, int y) {
if (x + word.length() > board[0].size() || y - word.length() < -1) {
return false; // Word too long to fit diagonally backward from top-right corner
}
for (int i = 0; i < word.length(); i++) {
if (board[y - i][x + i] != '.' && board[y - i][x + i] != word[i]) {
return false; // Conflict with existing letter
}
}
for (int i = 0; i < word.length(); i++) {
board[y - i][x + i] = word[i];
}
return true;
}
bool checkWordsDontAppear(const std::vector<std::vector<char>>& board, const std::vector<std::string>& dont_appear) {
// Convert the vector of words to a set for faster lookup
if (dont_appear.size() == 0){
return true;
}
std::set<std::string> dontAppearSet(dont_appear.begin(), dont_appear.end());
int height = board.size();
int width = board[0].size();
// Check horizontally
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::string word;
// Right
for (int k = j; k < width; ++k) {
word += board[i][k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
word.clear();
// Left
for (int k = j; k >= 0; --k) {
word += board[i][k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
}
}
// Check vertically
for (int j = 0; j < width; ++j) {
for (int i = 0; i < height; ++i) {
std::string word;
// Down
for (int k = i; k < height; ++k) {
word += board[k][j];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
word.clear();
// Up
for (int k = i; k >= 0; --k) {
word += board[k][j];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
}
}
// Check diagonally (top-left to bottom-right)
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::string word;
// Bottom-right
for (int k = 0; i + k < height && j + k < width; ++k) {
word += board[i + k][j + k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
word.clear();
// Top-left
for (int k = 0; i - k >= 0 && j - k >= 0; ++k) {
word += board[i - k][j - k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
}
}
// Check diagonally (top-right to bottom-left)
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::string word;
// Bottom-left
for (int k = 0; i + k < height && j - k >= 0; ++k) {
word += board[i + k][j - k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
word.clear();
// Top-right
for (int k = 0; i - k >= 0 && j + k < width; ++k) {
word += board[i - k][j + k];
if (dontAppearSet.find(word) != dontAppearSet.end()) {
return false; // Word found in don't appear list
}
}
}
}
// If none of the words from the don't appear list are found, return true
return true;
}
void insert_words(std::set<std::vector<std::vector<char>>>& solution,std::vector<std::vector<char>>& board,std::vector<std::string>& dont_appear,std::vector<std::string>& words, int width, int height, int index){
std::vector<std::vector<char>> board1 = board;
if (index >= words.size()) {
if (checkWordsDontAppear(board,dont_appear)){
solution.insert(board);
}
return;
}
//loops through every posiiton to see if it can fit even in the first case when it is backtracing
for (int y = 0;y < height; y++){
for (int x = 0; x < width; x++){
//if one word cannot fit into any of the cases then it wont even recurse
if (horizontal(words[index], board1, x,y)){
// Reset x if it exceeds the width
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
//goes back to the original state before any word is inserted tot see if it can inserted another
board1 = board;
if (horizontal_backward(words[index], board1, x,y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if (vertical(words[index], board1, x, y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if (vertical_backward(words[index], board1, x, y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if (diagonal_top_left_corner(words[index], board1, x,y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if (diagonal_top_left_corner_backward(words[index], board1, x,y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if(diagonal_top_right_corner(words[index], board1, x,y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
if (diagonal_top_right_corner_backward(words[index], board1, x,y)){
insert_words(solution,board1,dont_appear, words, width, height, index+1);
}
board1 = board;
}
}
}
void outputBoard(const std::set<std::vector<std::vector<char>>>& solutionSets, std::ofstream& output, const std::string& solutionType) {
if (solutionType == "one_solution") {
if (!solutionSets.empty()) {
const std::vector<std::vector<char>>& firstSolution = *(solutionSets.begin());
output << "Board:" << '\n';
for (size_t y = 0; y < firstSolution.size(); ++y) {
for (size_t x = 0; x < firstSolution[y].size(); ++x) {
output << firstSolution[y][x];
}
output << '\n';
}
}
return;
}
// Output each solution set to the file
output << solutionSets.size() << " solution(s)" << std::endl;
for (const std::vector<std::vector<char>>& solution : solutionSets) {
output << "Board:" << '\n';
for (size_t y = 0; y < solution.size(); ++y) {
for (size_t x = 0; x < solution[y].size(); ++x) {
output << solution[y][x];
}
output << '\n';
}
}
}
std::vector<std::vector<std::string>> createStartingWordVariations(const std::vector<std::string>& originalVector) {
std::vector<std::vector<std::string>> variations;
for (size_t i = 0; i < originalVector.size(); ++i) {
std::vector<std::string> variation;
for (size_t j = i; j < originalVector.size(); ++j) {
variation.push_back(originalVector[j]);
}
for (size_t j = 0; j < i; ++j) {
variation.push_back(originalVector[j]);
}
variations.push_back(variation);
}
return variations;
}
int main(int argc, char* argv[]) {
std::string puzzle_file_name = argv[1];
std::string output_file_name = argv[2];
std::string one_or_all_soultion = argv[3];
std::ifstream puzzle(puzzle_file_name);
if(!puzzle.is_open()){
std::cerr << "could not open puzzle file";
}
std::ofstream output(output_file_name);
if(!output.is_open()){
std::cerr << "could not open output" << std::endl;
}
std::vector<std::string> appear;
std::vector<std::string> dont_appear;
int height;
int width;
parsefile(appear,dont_appear,puzzle,height,width);
std::vector<std::vector<std::string>> differentStart;
std::vector<std::vector<char>> board;
for(int y = 0; y < height; y++){
std::vector<char> row; // Create a new row vector for each y
for(int x = 0; x < width; x++){
row.push_back('.'); // Add 'a' to each element of the row
}
board.push_back(row); // Add the row to the board
}
differentStart = createStartingWordVariations(appear);
// maybe you can just print the flipped grids of the original
std::set<std::vector<std::vector<char>>> solutionSets;
for (int i = 0; i < differentStart.size(); i++){
insert_words(solutionSets,board,dont_appear,differentStart[i],width,height,0);
}
outputBoard(solutionSets,output,one_or_all_soultion);
return 0;
}