Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unique hats
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
template <class T>
void print(vector<T> items) {
for(T item:items)
cout << item << " ";
cout << endl;
}
template <class T>
bool is_unique(vector<T> items) {
if(items.size() == 0)
return false;
unordered_set<T> myset;
for(T item:items)
myset.insert(item);
return (items.size() == myset.size());
}
void print_unique_hats(vector<vector<int>> &men_hats, int row, int size, vector<int> &collection) {
// sanity
if(row == size)
return;
auto hats = men_hats[row];
for(auto hat:hats) {
collection.push_back(hat);
if(collection.size() == size)
{
if(is_unique<int>(collection))
print<int>(collection);
}
else {
print_unique_hats(men_hats, row + 1, size, collection);
}
collection.pop_back();
}
return;
}
int main() {
int total_men = 3;
vector<vector<int>> men_hats({{5, 100, 1}, {2}, {5, 100}});
vector<int> collection;
print_unique_hats(men_hats, 0, total_men, collection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.