-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
250 lines (216 loc) · 7.08 KB
/
main.cpp
File metadata and controls
250 lines (216 loc) · 7.08 KB
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
// 2339 Solutions
#include <iomanip>
#include <cstring>
#include <fstream>
#include <time.h>
#include "orientationCalculations.h"
using namespace std;
const int height = 6, width = 10;
int grid[height][width] = {0};
int testGrid[height][width]; // For isSolution() and isSpaceNotMultipleOfFive()
set<set<vector<vector<int>>>> shapesUsedInCorner; // Shapes used in initial corner
set<string> solutionCodes; // Set of solutions found so far
int numSolutions = 0;
ofstream myFile("solutions.txt");
// Each real solution will have four "solutions", since it can be flipped horiztonally and vertically
// Codes are a simplified version of the board
string generateCode(int someGrid[height][width]) {
string code;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if ((i == height-1) || (someGrid[i][j] != someGrid[i+1][j]))
code.push_back('1'); // _
else
code.push_back('0');
if ((j == width-1) || (someGrid[i][j] != someGrid[i][j+1]))
code.push_back('1'); // |
else
code.push_back('0');
}
}
return code;
}
// Determine if the solution is a unique solution or whether a flipped version of it has already been found
bool isSolution() {
string codes[4];
memcpy(testGrid, grid, height*width*sizeof(int));
for (int k = 0; k < 2; k++) {
// Flip horizontally
for (int i = 0; i < height; i++) {
for (int j = 0; j < width/2; j++) {
int temp = testGrid[i][j];
testGrid[i][j] = testGrid[i][width-j-1];
testGrid[i][width-j-1] = temp;
}
}
codes[2*k] = generateCode(testGrid);
// Flip horizontally
for (int i = 0; i < height/2; i++) {
for (int j = 0; j < width; j++) {
int temp = testGrid[i][j];
testGrid[i][j] = testGrid[height-i-1][j];
testGrid[height-i-1][j] = temp;
}
}
codes[2*k+1] = generateCode(testGrid);
}
// See if any version of the current solution has already been found
for (int i = 0; i < 4; i++) {
if (solutionCodes.find(codes[i]) != solutionCodes.end())
return false;
}
// This is a new solution, so add it to the set of new solutions
solutionCodes.insert(codes[3]);
numSolutions++;
return true;
}
void printSolution(ostream& out) {
out << "Solution " << numSolutions << endl;
for (int i = 0; i < width; i++)
out << " _"; // Print top
out << endl;
for (int i = 0; i < height; i++) {
out << "|";
for (int j = 0; j < width; j++) {
if ((i == height-1) || (grid[i][j] != grid[i+1][j]))
out << "_";
else
out << " ";
if ((j == width-1) || (grid[i][j] != grid[i][j+1]))
out << "|";
else
out << " ";
}
out << endl;
}
out << endl;
}
// Used for debugging
void printGrid() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
}
// Recursive function, used to find amount of connected empty space for isSpaceNotMultipleOfFive()
int recurseEmpty(int x, int y) {
if (x<0 || x>=height || y<0 || y>=width || testGrid[x][y] != 0)
return 0;
testGrid[x][y] = 1;
return 1 + recurseEmpty(x+1,y) + recurseEmpty(x-1,y) + recurseEmpty(x,y+1) + recurseEmpty(x,y-1);
}
// Additional optimization, to determine if the areas of remaining space are multiples of 5 or not
bool isSpaceNotMultipleOfFive(int numRemShapes) {
memcpy(testGrid, grid, height*width*sizeof(int));
int numEmpty = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (testGrid[i][j] == 0) {
numEmpty = recurseEmpty(i,j);
if (numEmpty % 5 != 0) // If the empty space isn't divisible by 5, it's impossible for shapes to fit in it
return true;
if (numEmpty == numRemShapes*5) // If the empty space is all of the empty space, there's no more empty space
return false;
}
}
}
return false;
}
// Remove shape from grid --> x,y is top left corner of shape
void removeShapeFromGrid(vector<vector<int>> shape, int x, int y) {
for (int i = 0; i < shape.size(); i++) {
for (int j = 0; j < shape[0].size(); j++) {
if (shape[i][j] == 1)
grid[x+i][y+j] = 0;
}
}
}
// Returns whether the point is in a corner of the grid
bool isCorner(int x, int y) {
return ((x==0 || x==height-1) && (y==0 || y==width-1));
}
// Checks if the shape will fit in the grid and the position
// Returns how much the shape is shifted to the left (-1 if it doesn't fit)
int checkFitAddShape(vector<vector<int>> shape, int x, int y, int numRemShapes, bool alreadyUsed) {
int shiftFactor = 0;
while (shape[0][shiftFactor] == 0)
shiftFactor++;
for (int i = 0; i < shape.size(); i++) {
for (int j = 0; j < shape[0].size(); j++) {
if (x+i < 0 || x+i >= height || y+j-shiftFactor < 0 || y+j-shiftFactor >= width) // If it's out of bounds
return -1;
if (grid[x+i][y+j-shiftFactor] >= 1 && shape[i][j] == 1) // If a shape is already there
return -1;
// If the shape has already been used in the initial corner, all solutions with it in any corner have been found
if (shape[i][j] == 1 && alreadyUsed && isCorner(x+i, y+j-shiftFactor)) {// Additional optimization
return -1;
}
}
}
// Add shape
y -= shiftFactor;
int shapeIndex = allShapes.size()+1-numRemShapes;
for (int i = 0; i < shape.size(); i++) {
for (int j = 0; j < shape[0].size(); j++) {
if (shape[i][j] == 1)
grid[x+i][y+j] = shapeIndex;
}
}
if (isSpaceNotMultipleOfFive(numRemShapes)) { // Additional optimization
removeShapeFromGrid(shape, x, y);
return -1;
}
return shiftFactor;
}
void addShape(int pos, set<set<vector<vector<int>>>> remainingShapes) {
if (remainingShapes.size() == 0) { // If all shapes added
if (isSolution()) {
printSolution(cout);
printSolution(myFile);
}
return;
}
while (grid[pos/width][pos%width] >= 1) // Move to next empty spot in grid
pos++;
int x = pos/width;
int y = pos%width;
for (auto shapeOrientations: remainingShapes) { // For each available shape
// Check if shape has been used in corner already
bool alreadyUsed = (shapesUsedInCorner.find(shapeOrientations) != shapesUsedInCorner.end());
for (auto shape : shapeOrientations) { // For each orientation
int shiftFactor = checkFitAddShape(shape, x, y, remainingShapes.size(), alreadyUsed);
if (shiftFactor >= 0) {
set<set<vector<vector<int>>>> nextRemainingShapes = remainingShapes;
nextRemainingShapes.erase(shapeOrientations);
addShape(pos+1, nextRemainingShapes);
removeShapeFromGrid(shape, x, y-shiftFactor);
}
}
if (pos == 0) { // Additional optimization, if in initial corner
shapesUsedInCorner.insert(shapeOrientations);
if (allShapes.size() - shapesUsedInCorner.size() < 4)
return; // All other solutions will have been found by now
}
}
}
int main()
{
if (myFile.is_open())
cout << "File open successfully" << endl;
else {
cout << "File unable to open, aborting..." << endl;
return 0;
}
calculateShapeOrientations();
clock_t start = clock();
addShape(0, allShapes);
clock_t end = clock();
cout << "Total solutions: " << numSolutions << endl;
myFile << "Total solutions: " << numSolutions << endl;
cout << "Calculation time (ms): " << (end-start)/(CLOCKS_PER_SEC/1000) << endl;
myFile << "Calculation time (s): " << (end-start)/(CLOCKS_PER_SEC) << endl;
myFile.close();
return 0;
}