Files
contests/2022/city/10/domino/domino_slow.cpp

144 lines
3.5 KiB
C++

#include <bits/stdc++.h>
using namespace std;
// ifstream in("test.in");
// ofstream out("test.out");
struct Domino {
int side1;
int side2;
};
// Global variables
int n;
int max_length = 1;
unordered_set<Domino*> used_dominos;
vector<Domino*> dominos_by_side[7];
// Recursive solution
void buildTrain(int next_side, int current_length) {
// cout << "Building train; Current length: " << current_length << "; next side: " << next_side << endl;
if (current_length > max_length) {
max_length = current_length;
// Debug print
// cout << "updated to " << current_length << endl;
// if (max_length == 8) {
// cout << "found 8" << endl;
// for (auto it = used_dominos.begin(); it != used_dominos.end(); it++) {
// cout << (*it)->side1 << " " << (*it)->side2 << endl;
// }
// }
}
int stop = dominos_by_side[next_side].size();
for (int i = 0; i < stop; i++) {
// Looking at Domino dominos_by_side[next_side][i]
#define matched_domino dominos_by_side[next_side][i]
// Skip if already part of the train
if (used_dominos.count(matched_domino) > 0) {
continue;
}
// The logic for elongating the train
used_dominos.insert(matched_domino);
// Figure out if the domino matched by side1 or side2:
int other_side;
if ((matched_domino->side1) == next_side) {
other_side = matched_domino->side2;
} else {
other_side = matched_domino->side1;
}
// The recursion
buildTrain(other_side, current_length + 1);
// Deconstruct
used_dominos.erase(matched_domino);
}
}
int main(int argc, char *argv[]) {
string inName = "test.in";
string outName = "test.out";
if (argc > 1) {
inName = argv[1];
cout << "Using \"" << inName << "\" for test name" << endl;
} else {
cout << "Warning: no in name provided, defaulting to test.in" << endl;
}
if (argc > 2) {
outName = argv[2];
cout << "Using \"" << outName << "\" for out name" << endl;
} else {
cout << "Warning: no out name proviced, defaulting to test.out" << endl;
}
ifstream in(inName);
ofstream out(outName);
in >> n;
// cout << "n: " << n << endl;
if (n < 2) {
out << "1";
in.close();
out.close();
return 0;
}
Domino* dominos[n];
for (int i = 0; i < n; i++) {
Domino* domino = new Domino();
in >> (*domino).side1;
in >> (*domino).side2;
dominos[i] = domino;
dominos_by_side[(domino -> side1)].push_back(domino);
dominos_by_side[(domino -> side2)].push_back(domino);
}
// Debug print
for (int j = 0; j < n; j++) {
cout << dominos[j] << endl;
cout << (dominos[j]->side1) << " " << (dominos[j]->side2) << endl;
}
used_dominos.clear();
// for (int i = 0; i < n; i++) {
// #define start_domino dominos[i]
// // cout << "starting with domino " << start_domino->side1 << " " << start_domino->side2 << "" << start_domino << endl;
// used_dominos.insert(start_domino);
// buildTrain(start_domino->side1, 1);
// buildTrain(start_domino->side2, 1);
// used_dominos.clear();
// }
used_dominos.insert(dominos[2]);
cout << "start " << dominos[2]->side1 << " " << dominos[2]->side2 << endl;
buildTrain(dominos[2]->side1, 1);
cout << "Result: " << max_length << endl;
return 0;
}