-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockgroup.cpp
279 lines (231 loc) · 9.11 KB
/
blockgroup.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
#include "blockgroup.h"
#include <set>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::set;
using std::cout;
using std::endl;
using std::pair;
using std::make_pair;
using std::vector;
using std::string;
using std::min;
using std::max;
BlockGroup::BlockGroup(bool orientation, bool subtractive, vector<pair<int, int>> v, Color c) : Object() {
oriented = orientation;
sub = subtractive;
pairs = set<pair<int, int>>();
n = v.size();
bottomleft = {INT_MAX, INT_MAX};
topright = {INT_MIN, INT_MIN};
for (int i = 0; i < n; i++) {
pairs.insert(make_pair(v[i].first, v[i].second));
bottomleft.first = min(bottomleft.first, v[i].first);
bottomleft.second = min(bottomleft.second, v[i].second);
topright.first = max(topright.first, v[i].first);
topright.second = max(topright.second, v[i].second);
}
boundingbox = {topright.first - bottomleft.first + 1, topright.second - bottomleft.second + 1};
color = c;
}
BlockGroup::BlockGroup(bool orientation, bool subtractive, vector<pair<int, int>> v) : Object() {
oriented = orientation;
sub = subtractive;
pairs = set<pair<int, int>>();
n = v.size();
bottomleft = {INT_MAX, INT_MAX};
topright = {INT_MIN, INT_MIN};
for (int i = 0; i < n; i++) {
pairs.insert(make_pair(v[i].first, v[i].second));
bottomleft.first = min(bottomleft.first, v[i].first);
bottomleft.second = min(bottomleft.second, v[i].second);
topright.first = max(topright.first, v[i].first);
topright.second = max(topright.second, v[i].second);
}
boundingbox = {topright.first - bottomleft.first + 1, topright.second - bottomleft.second + 1};
color = YELLOW;
}
void BlockGroup::updateBounds() {
bottomleft = {INT_MAX, INT_MAX};
topright = {INT_MIN, INT_MIN};
for (auto p : pairs) {
bottomleft.first = min(bottomleft.first, p.first);
bottomleft.second = min(bottomleft.second, p.second);
topright.first = max(topright.first, p.first);
topright.second = max(topright.second, p.second);
}
boundingbox = {topright.first - bottomleft.first + 1, topright.second - bottomleft.second + 1};
}
// Utility Functions
bool BlockGroup::contains(pair<int, int> p) {
return pairs.find(p) != pairs.end();
}
void BlockGroup::add(pair<int, int> p) {
if (contains(p)) return;
pairs.insert(p);
n++;
return;
}
void BlockGroup::remove(pair<int, int> p) {
if (!contains(p)) return;
pairs.erase(pairs.find(p));
n--;
return;
}
void BlockGroup::reset(vector<pair<int, int>>& p) {
n = p.size();
pairs.clear();
bottomleft = {INT_MAX, INT_MAX};
topright = {INT_MIN, INT_MIN};
for (pair<int, int> x : p) {
pairs.insert(x);
bottomleft.first = min(bottomleft.first, x.first);
bottomleft.second = min(bottomleft.second, x.second);
topright.first = max(topright.first, x.first);
topright.second = max(topright.second, x.second);
}
boundingbox = {topright.first - bottomleft.first + 1, topright.second - bottomleft.second + 1};
}
BlockGroup BlockGroup::clone() {
vector<pair<int, int>> res;
for (auto i : pairs) res.push_back(make_pair(i.first, i.second));
return BlockGroup(oriented, sub, res);
}
// General Functions
void BlockGroup::rotate(int x) {
while (x < 0) x += 1000;
x %= 4;
if (x <= 0) return;
rotate(x - 1);
vector<pair<int, int>> res;
for (auto i : pairs) res.push_back(make_pair(-1 * i.second, i.first));
reset(res);
}
void BlockGroup::move(pair<int, int> p) {
vector<pair<int, int>> res;
for (auto i : pairs) res.push_back(make_pair(i.first + p.first, i.second + p.second));
reset(res);
}
void BlockGroup::invmov(pair<int, int> p) {
vector<pair<int, int>> res;
for (auto i : pairs) res.push_back(make_pair(i.first - p.first, i.second - p.second));
reset(res);
}
void BlockGroup::normalize() {
invmov(bottomleft);
}
void BlockGroup::removeRegion(BlockGroup x) {
for (pair<int, int> p : x.pairs) remove(p);
}
void BlockGroup::addRegion(BlockGroup x) {
for (pair<int, int> p : x.pairs) add(p);
}
string BlockGroup::to_string() {
string res = "[O = ";
res = res.append(oriented ? "1" : "0");
res = res + " S = ";
res = res.append(sub ? "1" : "0");
res = res + " N = ";
res = res.append(std::to_string(n));
res = res + "] = [";
for (auto i : pairs) {
res = res + "<";
res = res.append(std::to_string(i.first));
res = res + " ";
res = res.append(std::to_string(i.second));
res = res + ">";
}
return res + "]";
}
void BlockGroup::disp() {
cout << to_string() << endl;
}
// Region testing
bool BlockGroup::containsbb(BlockGroup b) { // Can the bounding box contain that belonging to region b?
if (boundingbox.first < b.boundingbox.first) return false;
if (boundingbox.second < b.boundingbox.second) return false;
return true;
}
bool BlockGroup::directoverlay(BlockGroup b) { // Does this region contain region b in terms of absolute coordinates?
if (n < b.n) return false;
for (pair<int, int> p : b.pairs) {
if (!contains(p)) return false;
}
return true;
}
vector<pair<int, int>> BlockGroup::fixedoverlay(BlockGroup b) { // Does this region contain region b? Return the offset if yes, INT_MIN if no.
// This only affects one rotation.
if (n < b.n) return vector<pair<int, int>>();
// the starting offset is the absolute difference in bounding boxes
// the ending offset is the width of the smaller bounding box in the larger one
int dx = bottomleft.first - b.bottomleft.first;
int dy = bottomleft.second - b.bottomleft.second;
BlockGroup test = b.clone();
test.move({dx, dy});
// cout << "MOVED " << endl;
// disp();
// test.disp();
int width = abs(boundingbox.first - b.boundingbox.first);
int height = abs(boundingbox.second - b.boundingbox.second);
vector<pair<int, int>> res;
// cout << "EFFECTIVE BOX " << width << " " << height << endl;
for (int i = 0; i <= width; i++) {
for (int j = 0; j <= height; j++) {
// test.disp();
if (directoverlay(test)) res.push_back({i, j});
test.move({0, 1});
}
test.move({1, -1 * height - 1});
}
return res;
}
vector<vector<pair<int, int>>> BlockGroup::overlay(BlockGroup b) {
vector<vector<pair<int, int>>> res(4, vector<pair<int, int>>());
res[0] = fixedoverlay(b);
if (!b.oriented) {
BlockGroup test = b.clone();
test.normalize();
for (int i = 0; i < 3; i++) {
test.rotate(1);
test.normalize();
res[i + 1] = (fixedoverlay(test));
}
}
return res;
}
// Now for the real thing
bool BlockGroup::dfsUtil(BlockGroup region, vector<BlockGroup>& v, int index) {
// cout << index << " ";
// region.disp();
if ((size_t)index >= v.size()) return region.n == 0;
BlockGroup group = v[index].clone();
group.normalize();
vector<vector<pair<int, int>>> options = region.overlay(group);
bool res = false;
for (int i = 0; i < 4; i++) {
group.move(region.bottomleft);
for (auto op : options[i]) {
group.move(op);
region.removeRegion(group);;
res |= dfsUtil(region, v, index + 1);
if (res) return true;
region.addRegion(group);
group.invmov(op);
}
if (group.oriented) break;
group.rotate(1);
group.normalize();
}
return res;
}
bool BlockGroup::solve(vector<BlockGroup> v) {
int diff = n;
for (auto i : v) {
if (i.sub) diff += i.n;
else diff -= i.n;
}
if (diff != 0) return false;
return dfsUtil(clone(), v, 0);
}