-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
605 lines (493 loc) · 24.9 KB
/
grid.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include <queue>
#include <map>
#include <memory>
#include "grid.h"
#include "object.h"
#include "util.h"
using std::queue;
using std::map;
using std::max;
using std::cout;
using std::endl;
using std::pair;
using std::make_pair;
Grid::Grid(vector<vector<std::shared_ptr<Object>>>& v) { // Once a grid is created it cannot be changed unless changes are consistent across all aspects.
n = v.size();
m = 0;
for (auto i : v) m = max((int)(i.size()), m);
if (n % 2 == 0) n++;
if (m % 2 == 0) m++;
board = vector<vector<std::shared_ptr<Object>>>(n, vector<std::shared_ptr<Object>>(m));
starts = set<pair<int, int>>();
ends = set<pair<int, int>>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((size_t)j < v[i].size()) board[i][j] = v[i][j];
if (isStartingPoint(board[i][j])) {
begin = {i, j};
starts.insert({i, j});
}
if (isEndingPoint(board[i][j])) ends.insert({i, j});
if (instanceof<Dot>(board[i][j])) dots.insert({i, j});
if (instanceof<Blob>(board[i][j])) blobs.insert({i, j});
if (instanceof<Star>(board[i][j])) stars.insert({i, j});
if (instanceof<Triangle>(board[i][j])) triangles.insert({i, j});
if (instanceof<BlockGroup>(board[i][j])) blocks.insert({i, j});
if (instanceof<Cancel>(board[i][j])) cancels.insert({i, j});
}
}
}
void Grid::defaultGrid() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i % 2 == 0 || j % 2 == 0) (board[i][j])->isPath = true;
}
}
}
void Grid::drawLine(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) {
if (a.second > b.second) swap(a, b);
for (int i = a.second; i <= b.second; i++) board[a.first][i]->isPathOccupied = true;
}
else if (a.second == b.second) {
if (a.first > b.first) swap(a, b);
for (int i = a.first; i <= b.first; i++) board[i][a.second]->isPathOccupied = true;
}
}
void Grid::drawPath(vector<pair<int, int>> v) {
if (v.size() < 2) return;
for (int i = 1; (size_t)i < v.size(); i++) drawLine(v[i - 1], v[i]);
}
Grid::Grid() {
}
Grid::~Grid() {
for (int i = 0; (size_t)i < board.size(); i++) {
for (int j = 0; (size_t)j < board[i].size(); j++) {
// delete board[i][j];
}
}
}
string Grid::to_string() {
string s = "";
for (auto i : board) {
for (auto j : i) {
char open = j->isPathOccupied ? '[' : (j->isPath ? '+' : '_');
char close = j->isPathOccupied ? ']' : (j->isPath ? '+' : '_');
s = s + open + get_type(j) + close + " ";
}
s = s + "\n";
}
return s.substr(0, s.length() - 1);
}
void Grid::disp() {
cout << to_string() << endl;
}
// The verification algorithm
bool Grid::inside(pair<int, int> p) {
if (p.first < 0 || p.second < 0) return false;
if ((size_t)(p.first) >= board.size() || (size_t)(p.second) >= board[p.first].size()) return false;
return true;
}
bool Grid::ver(int sx, int sy) {
// cout << "VERIFYING GRID" << endl;
// The algorithm works in four stages: THE FOX / THE WOLF / THE DRUDE / THE PHOENIX
// The first section denoted THE FOX begins by handling the more trivial matters.
// Just as foxes are easily recognizable (and a common Fursona species), the tasks for this section are relatively self-explanatory and easy to check.
// Namely, whether the path goes from a start to an end, whether all dots are covered, and whether triangles are solved.
// Violations do not automatically return False. Instead, the locations of violated symbols are put into a set.
// However, this is arguably the most important section because it establishes the trajectory of the path.
const int dx[4] = {01, 00, -1, 00};
const int dy[4] = {00, 01, 00, -1};
if (!isStartingPoint(board[sx][sy])) return false;
std::shared_ptr<Object> o = board[sx][sy];
if (!o->isPathOccupied) return false;
// cout << "BASIC CHECK COMPLETED";
set<pair<int, int>> vis;
queue<pair<int, int>> q;
q.push({sx, sy});
bool reachedend = false;
while (q.size() > 0) {
pair<int, int> p = q.front();
q.pop();
vis.insert(p);
for (int i = 0; i < 4; i++) {
pair<int, int> next = {p.first + dx[i], p.second + dy[i]};
if (!inside(next)) continue;
std::shared_ptr<Object> n = board[next.first][next.second];
if (isEndingPoint(n)) reachedend = true;
if (!n->isPath || !n->isPathOccupied) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
// for (auto i : vis) cout << i.first << " " << i.second << endl;
if (!reachedend) return false;
// cout << "REACHED END" << endl;
set<pair<int, int>> violations;
for (auto i : dots) {
std::shared_ptr<Object> o = board[i.first][i.second];
if (!o->isPathOccupied) violations.insert(i);
}
for (auto i : triangles) {
std::shared_ptr<Object> o = board[i.first][i.second];
if (!instanceof<Triangle>(o)) continue;
int target = (std::dynamic_pointer_cast<Triangle>(o))->x;
int count = 0;
for (int ii = 0; ii < 4; ii++) {
int xp = i.first + dx[ii];
int yp = i.second + dy[ii];
if (!inside({xp, yp})) continue;
std::shared_ptr<Object> o2 = board[xp][yp];
if (o2->isPath && o2->isPathOccupied) count++;
}
if (count != target) violations.insert(i);
}
// In the same way that wolves are largely territorial animals...
// The second section denoted THE WOLF ensures that colors and territories are properly partitioned.
// Namely, the blobs and the stars. Blobs cannot exist with any other color blob.
// And stars, while they can exist with other colors, must exist with exactly one other of their color.
// Although dots can be set as color, they do not affect the results.
// When we traverse this time, the dx/dy effect is doubled.
// However at each step we must also check the in between to make sure we do not hit the path.
// The SET only contains locations of non-path coordinates.
// Violations are measured as the lesser violation. Thus if a region contains 2 blue and 3 white dots
// Then the 2 blue dots are marked as violation. Cancellation symbols will also ``seek'' the blue dots.
map<Color, vector<std::shared_ptr<Object>>> ding; // List of colors
map<Color, int> selectedcolors;
set<pair<int, int>> collected;
vis.clear();
for (auto ii : blobs) {
if (vis.find(ii) != vis.end()) continue;
ding.clear();
selectedcolors.clear();
collected.clear();
queue<pair<int, int>> q;
q.push(ii);
while (q.size() > 0) {
pair<int, int> now = q.front();
q.pop();
vis.insert(now);
// cout << now.first << " / " << now.second << endl;
std::shared_ptr<Object> cur = board[now.first][now.second];
if (instanceof<Blob>(cur)) {
if (ding.find(cur->color) == ding.end()) ding.insert({cur->color, vector<std::shared_ptr<Object>>()});
(*(ding.find(cur->color))).second.push_back(cur);
}
if (instanceof<Blob>(cur)) {
collected.insert(now);
if (selectedcolors.find(cur->color) == selectedcolors.end()) selectedcolors.insert({cur->color, 0});
(*(selectedcolors.find(cur->color))).second++;
}
for (int i = 0; i < 4; i++) {
pair<int, int> mid = {now.first + dx[i], now.second + dy[i]};
pair<int, int> next = {now.first + dx[i] * 2, now.second + dy[i] * 2};
if (!inside(mid) || !inside(next)) continue;
std::shared_ptr<Object> between = board[mid.first][mid.second];
// std::shared_ptr<Object> hit = board[next.first][next.second];
if (between->isPathOccupied) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
/*
cout << "BLOB FOR BLOB " << ii.first << " " << ii.second << endl;
for (auto i : vis) cout << i.first << " " << i.second << endl;
cout << "COLORS!!!" << endl;
for (auto i : ding) {
cout << i.first << endl;
for (auto x : i.second) cout << get_type(x) << endl;
}
cout << "BLOB COLORS!!!" << endl;
for (auto i : selectedcolors) {
cout << i.first << " = " << i.second << endl;
}
*/
// Now we determine where the violations were.
// Among all colors that have a blob, the highest one ``wins'' and the others ``lose''.
// Ties are broken arbitrarily for now.
Color maxcolor = NIL;
int maxfreq = -1;
for (auto i : selectedcolors) {
if (ding.find(i.first) == ding.end()) continue;
int truefreq = ding.at(i.first).size();
if (truefreq > maxfreq) {
maxfreq = truefreq;
maxcolor = i.first;
}
}
bool hasmorecolors = false;
for (auto i : ding) {
if (i.first != maxcolor && i.first != NIL) hasmorecolors = true;
}
// cout << "AND THE WINNING COLOR IS " << maxcolor << endl;
for (auto i : collected) {
if (board[i.first][i.second]->color != maxcolor && board[i.first][i.second]->color != NIL) violations.insert(i);
else if (hasmorecolors) violations.insert(i);
}
}
// The other task of THE WOLF is to handle stars. Thankfully, these are easier.
vis.clear();
for (auto ii : stars) {
if (vis.find(ii) != vis.end()) continue;
ding.clear();
selectedcolors.clear();
collected.clear();
queue<pair<int, int>> q;
q.push(ii);
while (q.size() > 0) {
pair<int, int> now = q.front();
q.pop();
vis.insert(now);
// cout << now.first << " / " << now.second << endl;
std::shared_ptr<Object> cur = board[now.first][now.second];
if (ding.find(cur->color) == ding.end()) ding.insert({cur->color, vector<std::shared_ptr<Object>>()});
(*(ding.find(cur->color))).second.push_back(cur);
if (instanceof<Star>(cur)) {
collected.insert(now);
if (selectedcolors.find(cur->color) == selectedcolors.end()) selectedcolors.insert({cur->color, 0});
(*(selectedcolors.find(cur->color))).second++;
}
for (int i = 0; i < 4; i++) {
pair<int, int> mid = {now.first + dx[i], now.second + dy[i]};
pair<int, int> next = {now.first + dx[i] * 2, now.second + dy[i] * 2};
if (!inside(mid) || !inside(next)) continue;
std::shared_ptr<Object> between = board[mid.first][mid.second];
// std::shared_ptr<Object> hit = board[next.first][next.second];
if (between->isPathOccupied) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
/*
cout << "STAR FOR STAR " << ii.first << " " << ii.second << endl;
for (auto i : vis) cout << i.first << " " << i.second << endl;
cout << "COLORS!!!" << endl;
for (auto i : ding) {
cout << i.first << endl;
for (auto x : i.second) cout << get_type(x) << endl;
}
cout << "STAR COLORS!!!" << endl;
for (auto i : selectedcolors) {
cout << i.first << " = " << i.second << endl;
}
*/
for (auto i : collected) {
Color x = board[i.first][i.second]->color;
if (ding.find(x) == ding.end()) {
violations.insert(i);
continue;
}
if (ding.at(x).size() != 2) violations.insert(i);
}
}
// In German folklore, drudes are the malevolent nocturnal spirits associated with nightmares.
// This section aka. THE DRUDE was an absolute nightmare to implement. I am, of course, referring to polynominos.
// The algorithm is so weird that it has its own header file called blockgroup.h
// Simply put, it is brute force. After all, this problem is NP-complete.
// What happens here is simply a partition of the board and a check.
vis.clear();
set<pair<int, int>> region;
for (auto ii : blocks) {
if (vis.find(ii) != vis.end()) continue;
region.clear();
collected.clear();
queue<pair<int, int>> q;
q.push(ii);
while (q.size() > 0) {
pair<int, int> now = q.front();
q.pop();
vis.insert(now);
region.insert(now);
// cout << now.first << " / " << now.second << endl;
std::shared_ptr<Object> cur = board[now.first][now.second];
if (instanceof<BlockGroup>(cur)) collected.insert(now);
for (int i = 0; i < 4; i++) {
pair<int, int> mid = {now.first + dx[i], now.second + dy[i]};
pair<int, int> next = {now.first + dx[i] * 2, now.second + dy[i] * 2};
if (!inside(mid) || !inside(next)) continue;
std::shared_ptr<Object> between = board[mid.first][mid.second];
// std::shared_ptr<Object> hit = board[next.first][next.second];
if (between->isPathOccupied) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
/*
cout << "TETRIS LOCATIONS" << endl;
for (auto i : collected) cout << i.first << " " << i.second << endl;
cout << "REGION" << endl;
for (auto i : region) cout << i.first << " " << i.second << endl;
*/
vector<pair<int, int>> regionvec;
for (auto i : region) regionvec.push_back(make_pair((i.second)>>1, -1 * (i.first)>>1));
BlockGroup testregion = BlockGroup(1, 0, regionvec);
vector<BlockGroup> pieces;
for (auto i : collected) {
std::shared_ptr<Object> o = board[i.first][i.second];
if (!instanceof<BlockGroup>(o)) continue;
std::shared_ptr<BlockGroup> bg = std::dynamic_pointer_cast<BlockGroup>(o);
pieces.push_back(*bg);
}
if (testregion.solve(pieces));
else {
for (auto i : collected) violations.insert(i);
}
}
// THE PHOENIX is an immortal creature, one that rises from the ashes once its life is over.
// Similarly,we must start again from scratch when we encounter a cancellation symbol.
// The way we do this is by removing it and another symbol then rechecking our current path against the new grid.
// If there are no cancels the method simply returns what we have now.
if (cancels.size() == 0 || ignored.size() == cancels.size()) {
// cout << "NET VIOLATIONS - NON-CANCELLED ENDING\n";
// for (auto i : violations) cout << i.first << " " << i.second << endl;
return violations.size() == 0;
}
// cout << "NET VIOLATIONS - MOVING TO CANCELS..." << endl;
// for (auto i : violations) cout << i.first << " " << i.second << endl;
if (violations.size() == 0) return false; // There are cancels!!!
vis.clear();
for (auto ii : cancels) {
if (ignored.find(ii) != ignored.end()) continue;
collected.clear();
queue<pair<int, int>> q;
q.push(ii);
while (q.size() > 0) {
pair<int, int> now = q.front();
q.pop();
vis.insert(now);
region.insert(now);
// cout << now.first << " / " << now.second << endl;
std::shared_ptr<Object> cur = board[now.first][now.second];
if (isSymbol(cur) && !instanceof<Cancel>(cur)) {
if (violations.find(now) != violations.end()) collected.insert(now);
}
for (int i = 0; i < 4; i++) {
pair<int, int> next = {now.first + dx[i], now.second + dy[i]};
if (!inside(next)) continue;
std::shared_ptr<Object> hit = board[next.first][next.second];
if (hit->isPathOccupied) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
ignored.insert(ii);
(std::dynamic_pointer_cast<Cancel>(board[ii.first][ii.second]))->ignored = true;
// cout << "SYMBOL LOCATIONS FOR CANCEL " << ii.first << " " << ii.second << endl;
// cout << "CANCEL STATUS " << cancels.size() << " " << ignored.size() << endl;
bool retval = false;
for (auto i : collected) {
// cout << i.first << " " << i.second << endl;
std::shared_ptr<Object> o = board[i.first][i.second];
board[i.first][i.second] = o->clear();
if (instanceof<Dot>(o)) dots.erase(dots.find(i));
else if (instanceof<Star>(o)) stars.erase(stars.find(i));
else if (instanceof<Blob>(o)) blobs.erase(blobs.find(i));
else if (instanceof<Triangle>(o)) triangles.erase(triangles.find(i));
else if (instanceof<BlockGroup>(o)) blocks.erase(blocks.find(i));
// disp();
// cout << "VERIFYING MODIFIED..." << endl;
if (ver(sx, sy)) {
retval = true;
}
// cout << "FINISHED VERIFYING MODIFIED\n";
board[i.first][i.second] = o;
if (instanceof<Dot>(o)) dots.insert(i);
else if (instanceof<Star>(o)) stars.insert(i);
else if (instanceof<Blob>(o)) blobs.insert(i);
else if (instanceof<Triangle>(o)) triangles.insert(i);
else if (instanceof<BlockGroup>(o)) blocks.insert(i);
if (retval) break;
}
ignored.erase(ignored.find(ii));
(std::dynamic_pointer_cast<Cancel>(board[ii.first][ii.second]))->ignored = false;
// cout << "FINISHED CANCELLING...\n";
// disp();
if (retval) return true;
}
return false;
}
bool Grid::check() {
return ver(begin.first, begin.second);
}
// This function serves as a basic pruning system for the solver.
// validateRegion only checks for trivial things: blobs, triangles, dots.
// Cancels make this always return true.
// Obviously this is not the complete algorithm but merely a small thing to prune.
// If the path reaches a wall and it can go both ways, and if both regions fail this test...
// Then we prune.
// UPDATE - This test now tests blocks as well.
bool Grid::validateRegion(int sx, int sy, vector<pair<int, int>> ban) {
set<pair<int, int>> banned;
for (auto i : ban) banned.insert(i);
const int dx[4] = {01, 00, -1, 00};
const int dy[4] = {00, 01, 00, -1};
set<pair<int, int>> blobs;
set<pair<int, int>> triangles;
set<pair<int, int>> dots;
set<pair<int, int>> cancels;
set<pair<int, int>> blocks;
set<pair<int, int>> vis;
queue<pair<int, int>> q;
q.push({sx, sy});
while (q.size() > 0) {
pair<int, int> now = q.front();
q.pop();
vis.insert(now);
std::shared_ptr<Object> o = board[now.first][now.second];
if (instanceof<Blob>(o)) blobs.insert(now);
if (instanceof<Triangle>(o)) triangles.insert(now);
if (instanceof<Dot>(o)) dots.insert(now);
if (instanceof<Cancel>(o)) return true;
if (instanceof<BlockGroup>(o)) blocks.insert(now);
for (int i = 0; i < 4; i++) {
pair<int, int> next = {now.first + dx[i], now.second + dy[i]};
if (!inside(next)) continue;
std::shared_ptr<Object> hit = board[next.first][next.second];
if (hit->isPathOccupied) continue;
if (banned.find(next) != banned.end()) continue;
if (vis.find(next) != vis.end()) continue;
vis.insert(next);
q.push(next);
}
}
set<Color> colors;
for (auto i : blobs) colors.insert(board[i.first][i.second]->color);
if (colors.size() > 1) return false;
for (auto i : dots) if (!board[i.first][i.second]->isPathOccupied && banned.find(i) != banned.end()) return false;
for (auto i : triangles) {
std::shared_ptr<Object> o = board[i.first][i.second];
if (!instanceof<Triangle>(o)) continue;
int target = (std::dynamic_pointer_cast<Triangle>(o))->x;
int cnt = 0;
for (int d = 0; d < 4; d++) {
pair<int, int> sus = {i.first + dx[d], i.second + dy[d]};
if (!inside(sus)) continue;
if (board[sus.first][sus.second]->isPathOccupied || banned.find(sus) != banned.end()) cnt++;
}
if (cnt != target) return false;
}
// Test blocks
if (blocks.size() <= 0) return true;
vector<pair<int, int>> effectiveRegion;
for (auto i : vis) {
if (i.first % 2 == 0 || i.second % 2 == 0) continue;
effectiveRegion.push_back(make_pair(i.second / 2, -1 * i.first / 2));
}
vector<BlockGroup> boop;
for (auto i : blocks) {
std::shared_ptr<Object> o = board[i.first][i.second];
if (!instanceof<BlockGroup>(o)) continue;
boop.push_back(*(std::dynamic_pointer_cast<BlockGroup>(o)));
}
BlockGroup bg = BlockGroup(1, 0, effectiveRegion);
bg.normalize();
if (!bg.solve(boop)) {
return false;
}
return true;
}