-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPuzzle.cpp
522 lines (456 loc) · 11.3 KB
/
Puzzle.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <assert.h>
#include "Puzzle.h"
#include <algorithm>
using std::getline;
using std::string;
using std::endl;
using std::pair;
Puzzle::Puzzle()
{
m_vParts = NULL;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
for (int k = -1; k < 2; k++)
{
for (int s = -1; s < 2; s++)
{
m_mPartMap[make_pair(i, j)][make_pair(k, s)] = new list<Part>();
}
}
}
}
}
Puzzle::~Puzzle()
{
if (m_vParts)
delete m_vParts;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
for (int k = -1; k < 2; k++)
{
for (int s = -1; s < 2; s++)
{
delete m_mPartMap[make_pair(i, j)][make_pair(k, s)];
}
}
}
}
}
int Puzzle::init(std::string path)
{
int rc = 0;
size_t pos;
string line, token;
std::ifstream input;
std::vector<string> wrongIds, missingIds;
std::vector<pair<int, string>> wrongFormats;
input.open(path);
if (!input.is_open()) {
*fout << "Failed to open input file" << endl;
return -EINVAL;
}
getline(input, line);
pos = line.find("=");
if (pos != string::npos) {
token = line.substr(0, pos);
if (token.length() < 11 ||
token.compare(0, 11, "NumElements")) {
*fout << "Invalid first line in input file" << endl;
rc = -EINVAL;
}
else
line.erase(0, pos + 1);
token = line.substr(line.find_first_not_of(" "), string::npos);
std::istringstream buf(token);
buf >> m_iNumOfElements;
if (m_iNumOfElements < 1) {
*fout << "Invalid NumOfElements" << endl;
}
}
std::vector<Part> *Parts = new std::vector<Part>(m_iNumOfElements);
while (input && getline(input, line) && line.length()!=0) {
int id, left, up, right, down, eol;
std::istringstream iss;
iss.str(line);
iss >> id;
if (iss.fail()) {
wrongIds.emplace_back(std::to_string(id));
rc = -1;
continue;
}
if (id <= 0 || id > (int)m_iNumOfElements) {
wrongIds.emplace_back(std::to_string(id));
rc = -1;
continue;
}
iss >> left;
if (iss.fail() || abs(left)>1 ) {
wrongFormats.emplace_back(make_pair(id, line));
rc = -1;
continue;
}
iss >> up;
if(iss.fail() || abs(up)>1) {
wrongFormats.emplace_back(make_pair(id, line));
rc = -1;
continue;
}
iss >> right;
if(iss.fail() || abs(right)>1) {
wrongFormats.emplace_back(make_pair(id, line));
rc = -1;
continue;
}
iss >> down;
if(iss.fail() || abs(down)>1) {
wrongFormats.emplace_back(make_pair(id, line));
rc = -1;
continue;
}
iss >> eol;
if(!iss.fail() || !iss.eof()) {
wrongFormats.emplace_back(make_pair(id, line));
rc = -1;
continue;
}
Parts->at(id-1) = Part(id, left, up, right, down);
}
for (int i = 0; i < (int)Parts->size(); i++) {
Part& p = Parts->at(i);
if(!p.getId()) { //only missing, not wrong ID
missingIds.emplace_back(std::to_string(i+1));
rc = -1;
}
}
if ((int)missingIds.size() > 0)
{
bool printEndl = false;
bool firstRound = true;
for (int i = 0; i < (int)missingIds.size(); i++)
{
string deli =
(i == (int)missingIds.size() - 1) ? "" : ", ";
//print only if part is not a wrong-format ID
auto itr = find_if(wrongFormats.begin(), wrongFormats.end(),
[id = missingIds.at(i)](const auto& format) {
return std::to_string(format.first) == id;
});
if (itr == wrongFormats.end())
{
if (firstRound)
{
*fout << "Missing puzzle element(s) with the following IDs: ";
firstRound = false;
}
*fout << missingIds[i] << deli;
printEndl = true;
}
}
if(printEndl)
*fout << endl;
}
if ((int)wrongIds.size() > 0)
{
*fout << "Puzzle of size " << m_iNumOfElements << " cannot have the following IDs: ";
for (int i = 0; i < (int)wrongIds.size(); i++)
{
string deli =
(i == (int)wrongIds.size() - 1) ? "" : ", ";
*fout << wrongIds[i] << deli;
}
*fout << endl;
}
for (const pair<int, string> &line : wrongFormats)
{
*fout << "Puzzle ID " << line.first << " has wrong data: " << line.second << endl;
}
m_vParts = Parts;
return rc;
}
bool Puzzle::isValidStraightEdges(int sizei, int sizej)
{
int leftStraight = 0;
int topStraight = 0;
for (size_t i = 0; i < m_iNumOfElements; i++)
{
int left = (*m_vParts)[i].getLeft();
int top = (*m_vParts)[i].getTop();
if (left == 0)
leftStraight++;
if (top == 0)
topStraight++;
}
if (leftStraight < sizei || topStraight < sizej)
return false;
return true;
}
int Puzzle::solveRec(size_t i, size_t j, Table& tab)
{
int** table = tab.getTable();
int leftpeek, toppeek, rightpeek, bottompeek;
rightpeek = bottompeek = -2; //there is no part to the right/bottom
leftpeek = toppeek = 0; //there is a frame part to the left/top
if (j > 0 && table[i][j - 1] >= 0)
leftpeek = 0-((*m_vParts)[table[i][j - 1] - 1].getRight());
if (i > 0 && table[i - 1][j] >= 0)
toppeek = 0-((*m_vParts) [table[i-1][j]-1].getBottom());
if(j < (size_t)tab.getCols() - 1 && table[i][j + 1] != 0) //not empty
rightpeek = 0;
if (i < (size_t)tab.getRows() - 1 && table[i+1][j] != 0) //not empty
bottompeek = 0;
map<pair<int, int>, list<Part>*> currentMap= m_mPartMap[make_pair(leftpeek, toppeek)];
//We always check the top-left directions -
//solve the puzzle from top-left to bottom-right
for (auto const &m : currentMap)
{
//no parts of this left-top edges are avilable
if (m.second->empty())
continue;
//this is not the apropriate right-edge list of parts
if (rightpeek > -2 && rightpeek != m.first.first)
continue;
//this is not the apropriate bottom-edge list of parts
if (bottompeek > -2 && bottompeek != m.first.second)
continue;
//if we got so far, we have match in this list - continue checking this part
Part current = m.second->front();
m.second->pop_front();
table[i][j] = current.getId();
//End of table
if ((i == (size_t)tab.getRows() - 1) && (j == (size_t)tab.getCols() - 1))
return 0; //solve succeeded
//End of line
if (j == (size_t)tab.getCols() - 1)
{
if (solveRec(i + 1, 0, tab) == 0) //move to the next line, and first column
return 0;
else
{
table[i][j] = 0;
m.second->push_back(current);
continue;
}
}
else // "middle" cell in line
{
if (solveRec(i, j + 1, tab) == 0) //continue solving along the current line
return 0;
else
{
table[i][j] = 0;
m.second->push_back(current);
continue;
}
}
}
return -1;
}
Table Puzzle::Solve()
{
unsigned int i;
int ret;
unsigned int size = m_iNumOfElements;
for(i = 1; i <= size; i++) {
if (size % i == 0) {
if (!isValidStraightEdges(i, size / i))
continue;
Table table(i, size/i);
ret = solveRec(0, 0, table);
if (ret == 0)
return table;
}
}
*fout << "Cannot solve puzzle : it seems that there is no proper solution" << endl;
return Table();
}
bool Puzzle::cornerCheck(bool &tr, bool &tl, bool &br, bool &bl)
{
//the easy case - only one element
if (m_iNumOfElements == 1) {
Part& p = (*m_vParts)[0];
tr = p.getTop() == 0 && p.getRight() == 0;
tl = p.getTop() == 0 && p.getLeft() == 0;
br = p.getBottom() == 0 && p.getRight() == 0;
bl = p.getBottom() == 0 && p.getLeft() == 0;
return (tr && tl && br && bl);
}
//the regular case - different 4 corner parts
for (size_t i = 0; i < m_iNumOfElements; i++)
{
Part& pi = (*m_vParts)[i];
if (pi.getTop() == 0 && pi.getRight() == 0)
{
pi.setCorner(true);
for (size_t j = 0; j < m_iNumOfElements; j++)
{
Part& pj = (*m_vParts)[j];
if (!pj.isCorner() && pj.getTop() == 0 && pj.getLeft() == 0)
{
pj.setCorner(true);
for (size_t k = 0; k < m_iNumOfElements; k++)
{
Part& pk = (*m_vParts)[k];
if (!pk.isCorner() && pk.getBottom() == 0 && pk.getRight() == 0)
{
pk.setCorner(true);
for (size_t m = 0; m < m_iNumOfElements; m++)
{
Part& pm = (*m_vParts)[m];
if (!pm.isCorner() && pm.getBottom() == 0 && pm.getLeft() == 0)
{
pm.setCorner(true);
return true;
}
}
pk.setCorner(false);
}
}
pj.setCorner(false);
}
}
pi.setCorner(false);
}
}
//the row case - 2 corner parts
for (size_t i = 0; i < m_iNumOfElements; i++)
{
Part& pi = (*m_vParts)[i];
//if a single part isn't straight on top/bottom, it will fail
if (pi.getTop() != 0 || pi.getBottom() != 0)
break;
if (pi.getRight() == 0)
{
pi.setCorner(true);
for (size_t j = 0; j < m_iNumOfElements; j++)
{
Part& pj = (*m_vParts)[j];
if(!pj.isCorner() && pj.getLeft() == 0)
{
pj.setCorner(true);
return true;
}
}
pi.setCorner(false);
}
}
//the col case - 2 corner parts
for (size_t i = 0; i < m_iNumOfElements; i++)
{
Part& pi = (*m_vParts)[i];
//if a single part isn't straight on right/left, it will fail
if (pi.getLeft() != 0 || pi.getRight() != 0)
break;
if (pi.getTop() == 0)
{
pi.setCorner(true);
for (size_t j = 0; j < m_iNumOfElements; j++)
{
Part& pj = (*m_vParts)[j];
if (!pj.isCorner() && pj.getBottom() == 0)
{
pj.setCorner(true);
return true;
}
}
pi.setCorner(false);
}
}
// if we got so far, it means there is no available solution (not enough corners)!
for (size_t i = 0; i < m_iNumOfElements; i++)
{
Part& p = (*m_vParts)[i];
if (p.getTop() == 0 && p.getRight() == 0) {
tr = true;
continue;
}
if (p.getTop() == 0 && p.getLeft() == 0) {
tl = true;
continue;
}
if (p.getBottom() == 0 && p.getRight() == 0) {
br = true;
continue;
}
if (p.getBottom() == 0 && p.getLeft() == 0) {
bl = true;
continue;
}
}
return false;
}
int Puzzle::preProcess()
{
int sum = 0;
int topStraight = 0;
int bottomStraight = 0;
int leftStraight = 0;
int rightStraight = 0;
bool tr = false;
bool tl = false;
bool br = false;
bool bl = false;
int ret = 0;
for (size_t i = 0; i < m_iNumOfElements; i++)
{
int left = (*m_vParts)[i].getLeft();
int top = (*m_vParts)[i].getTop();
int right = (*m_vParts)[i].getRight();
int bottom = (*m_vParts)[i].getBottom();
if (left == 0)
leftStraight++;
if (top == 0)
topStraight++;
if (right == 0)
rightStraight++;
if (bottom == 0)
bottomStraight++;
sum += (*m_vParts)[i].getLeft();
sum += (*m_vParts)[i].getTop();
sum += (*m_vParts)[i].getRight();
sum += (*m_vParts)[i].getBottom();
}
if (!((topStraight == bottomStraight && topStraight != 0) && (leftStraight == rightStraight && leftStraight != 0)))
{
*fout << "Cannot solve puzzle: wrong number of straight edges" << endl;
ret = -1;
}
if (Puzzle::cornerCheck(tr, tl, br, bl) == false)
{
ret = -1;
if (tl == false)
*fout << "Cannot solve puzzle: missing corner element: TL" << endl;
if (tr == false)
*fout << "Cannot solve puzzle: missing corner element: TR" << endl;
if (bl == false)
*fout << "Cannot solve puzzle: missing corner element: BL" << endl;
if (br == false)
*fout << "Cannot solve puzzle: missing corner element: BR" << endl;
}
if (sum != 0)
{
*fout << "Cannot solve puzzle : sum of edges is not zero" << endl;
ret = -1;
}
return ret;
}
void Puzzle::initPartsMap()
{
int l, t, r, b;
for (size_t i = 0; i < m_iNumOfElements; i++)
{
Part& p = m_vParts->at(i);
l = p.getLeft();
t = p.getTop();
r = p.getRight();
b = p.getBottom();
m_mPartMap[make_pair(l,t)][make_pair(r,b)]->push_back(p);
}
}