PCLP2/lab08/03.cpp

129 lines
2.4 KiB
C++
Raw Normal View History

2020-05-10 12:24:25 +00:00
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Piece {
public:
int a, b, c, d;
Piece(int _a, int _b, int _c, int _d) {
a = _a;
b = _b;
c = _c;
d = _d;
}
void rot() {
int tmp = a;
a = b;
b = d;
d = c;
c = tmp;
}
};
void print(vector< vector<Piece> >* v);
void resolve(vector< vector<Piece> >* v, int n, int a = 0, int b = 0) {
if(a >= n || b >= n) {
// solution found
cout<<"------------------------------------------------------------------------------------\n\n\n";
print(v);
return;
}
for(int i=0; i<4; i++) {
v->at(a)[b].rot();
// check if fits
if(a > 0) {
if(v->at(a)[b].a != v->at(a-1)[b].d) {
// doesn't fit to the part on its top
continue;
}
}
if(b > 0) {
if(v->at(a)[b].b != v->at(a)[b-1].c) {
// doesn't fit to the part on its left
continue;
}
}
resolve(v, n, b == n-1 ? a+1 : a, b == n-1 ? 0 : b+1);
}
}
int main(){
cout<<"N = ";
int n;
cin>>n;
vector< vector<Piece> > v;
cout<<"Form:\n";
cout<<"+---------+\n";
cout<<"|\\ /|\n";
cout<<"| \\ A / |\n";
cout<<"| \\ / |\n";
cout<<"| B |X| C |\n";
cout<<"| / \\ |\n";
cout<<"| / D \\ |\n";
cout<<"|/ \\|\n";
cout<<"+---------+\n";
for(int i=0; i<n; i++) {
vector<Piece> row;
for(int j=0; j<n; j++) {
int a, b, c, d;
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
cout<<"C = ";
cin>>c;
cout<<"D = ";
cin>>d;
row.push_back(Piece(a, b, c, d));
}
v.push_back(row);
}
cout<<"Initial set:\n";
print(&v);
cout<<"Solutions:\n";
resolve(&v, n);
return 0;
}
void print_pattern(string pattern, int cnt) {
for(int i=0; i<cnt; i++) {
cout<<pattern<<" ";
}
cout<<"\n";
}
void print(vector< vector<Piece> >* v) {
for(vector< vector<Piece> >::iterator row = v->begin(); row < v->end(); row++) {
print_pattern("+---------+", row->size());
print_pattern("|\\ /|", row->size());
for(int i=0; i<row->size(); i++) {
cout<<"| \\ "<<row->at(i).a<<" / | ";
}
cout<<"\n";
print_pattern("| \\ / |", row->size());
for(int i=0; i<row->size(); i++) {
cout<<"| "<<row->at(i).b<<" |X| "<<row->at(i).c<<" | ";
}
cout<<"\n";
print_pattern("| / \\ |", row->size());
for(int i=0; i<row->size(); i++) {
cout<<"| / "<<row->at(i).d<<" \\ | ";
}
cout<<"\n";
print_pattern("|/ \\|", row->size());
print_pattern("+---------+", row->size());
cout<<"\n\n";
}
}