I'm trying to write a recursive function that takes in a vector of strings that contains
"1 forward", "2 forward", "rotate left", "2 backwards" ... etc
how can I write recursive function that creates a vector of every unique combination of commands?
Please define what you mean by "unique combination". Is "1 forward" is an unique combination? Or should it be several (How many?) commands in a row?
By unique combination I mean a vector of the same length. So, just "1 forward" is not a unique combination.
This is what I want but is there a way to do it recursively?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <vector>
#include <algorithm>
std::vector<std::vector<std::string> > recursive_permutation(std::vector<std::string> data)
{
if( std::next_permutation(data.begin(), data.end()) ) {
std::vector<std::vector<std::string>> temp = recursive_permutation(data);
temp.push_back(data);
return temp;
} else {
std::vector<std::vector<std::string>> result = {data};
return result;
}
}
std::vector<std::vector<std::string>> all_unique_combinations(std::vector<std::string> source)
{
std::sort(source.begin(), source.end());
return recursive_permutation(source);
}
|
Yay for excessive copy
Last edited on