Skip to content

Instantly share code, notes, and snippets.

@olligobber
Last active November 15, 2017 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olligobber/75b3d2c74e5e1fccc3d390a45bcdd19c to your computer and use it in GitHub Desktop.
Save olligobber/75b3d2c74e5e1fccc3d390a45bcdd19c to your computer and use it in GitHub Desktop.
Solves something
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class solution {
public:
int structure[8][8];
bool operator== (solution other) {
for (int i=0; i<64; ++i) {
if (this->structure[i%8][i/8] != other.structure[i%8][i/8]) return false;
}
return true;
}
};
vector<solution> solns;
class shape {
public:
bool structure[5][5];
void horizontal() {
bool out[5][5];
for (int i=0; i<5; ++i) {
for (int j=0; j<5; ++j) {
out[4-i][j] = structure[i][j];
}
}
for (int i=0; i<5; ++i) {
for (int j=0; j<5; ++j) {
structure[i][j] = out[i][j];
}
}
}
void diagonal() {
bool out[5][5];
for (int i=0; i<5; ++i) {
for (int j=0; j<5; ++j) {
out[j][i] = structure[i][j];
}
}
for (int i=0; i<5; ++i) {
for (int j=0; j<5; ++j) {
structure[i][j] = out[i][j];
}
}
}
} shapes[8];
int grid[8][8];
ofstream ofil;
void print_grid() {
for (int i=0; i<64; ++i) {
if (grid[i%8][i/8] < 0) cout << " ";
else cout << grid[i%8][i/8]+1 << " ";
if (i%8 == 7) cout << endl;
}
cout << endl;
}
int flood (int x, int y) {
int result = 1;
grid[x][y] = -2;
if (x > 0) {
if (grid[x-1][y] == -1) result += flood(x-1,y);
}
if (y > 0) {
if (grid[x][y-1] == -1) result += flood(x,y-1);
}
if (x < 7) {
if (grid[x+1][y] == -1) result += flood(x+1,y);
}
if (y < 7) {
if (grid[x][y+1] == -1) result += flood(x,y+1);
}
return result;
}
void tr (int this_shape) {
print_grid();
for (int rotation = 0; rotation < 8; ++rotation) {
for (int posx = -3; posx<7; ++posx) {
for (int posy = -3; posy<7; ++posy) {
bool do_break = false;
for (int i=0; i<64; ++i) {
if (grid[i%8][i/8] >= this_shape || grid[i%8][i/8] == -2) grid[i%8][i/8] = -1;
}
for (int i=0; i<25; ++i) {
if (shapes[this_shape].structure[i%5][i/5]) {
if (posx + i%5 > 7 || posy + i/5 > 7 || posx + i%5 < 0 || posy + i/5 < 0) {
do_break = true;
break;
} if (grid[posx + i%5][posy + i/5] > -1) {
do_break = true;
break;
}
grid[posx + i%5][posy + i/5] = this_shape;
}
}
if (do_break) continue;
for (int i = 0; i<64; ++i) {
if (grid[i%8][i/8] == -1) {
if (flood(i%8, i/8)%8 != 0) {
do_break = true;
break;
} else break;
}
}
if (do_break) continue;
//if (this_shape == 6) print_grid();
if (this_shape == 7) {
solution this_soln;
for (int i=0; i<64; ++i) {
this_soln.structure[i%8][i/8] = grid[i%8][i/8];
}
bool valid = true;
for (int i=0; i<solns.size(); ++i) {
if (this_soln == solns[i]) {
valid = false;
break;
}
}
solns.push_back(this_soln);
if (valid) {
for (int i=0; i<64; ++i) {
ofil << grid[i%8][i/8]+1 << " ";
if (i%8 == 7) ofil << endl;
}
ofil << endl;
//cout << "FOUND SOMETHING" << endl;
}
} else tr(this_shape + 1);
}
}
if (rotation % 2) shapes[this_shape].horizontal();
else shapes[this_shape].diagonal();
}
return;
}
int main () {
bool hold;
ifstream infil ("shapes.txt");
for (int i=0; i<8; ++i) {
for (int j=0; j<5; ++j) {
for (int k=0; k<5; ++k) {
infil >> hold;
shapes[i].structure[j][k] = hold;
}
}
}
infil.close();
for (int i=0; i<8; ++i) {
for (int j=0; j<8; ++j) {
grid[i][j] = -1;
}
}
ofil.open("out.txt");
tr(0);
ofil << solns.size();
ofil.close();
return 0;
}
1 1 0 0 0
1 1 0 0 0
1 1 0 0 0
1 0 0 0 0
1 0 0 0 0
1 1 1 0 0
1 1 1 0 0
1 0 0 0 0
1 0 0 0 0
0 0 0 0 0
1 1 1 1 0
1 0 0 1 0
1 0 0 1 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
1 1 0 0 0
0 1 1 0 0
0 1 1 0 0
0 0 0 0 0
1 1 1 0 0
1 1 1 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
1 1 0 0 0
1 1 0 0 0
1 1 0 0 0
1 1 0 0 0
0 0 0 0 0
1 1 1 0 0
1 1 1 0 0
1 1 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 0
0 1 1 0 0
0 1 1 0 0
0 0 0 0 0
0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment