I have what seems like a simple problem, but for the life of me, I can't figure it out. Basically, what i am looking for is how to find all permutations of a non-symmetrical matrix, where certain values have to remain in certain positions. The simplest way to explain this is with an example...
Let's say we have the following...
a b c
a
b c
d e f
In this matrix, we have to find all permutations where the first letter is either 'a', 'b', or 'c', the second letter is 'a', the third letter is 'b' or 'c', and the fourth letter is 'd', 'e' or 'f'. In our algorithm, the size of the matrix is not known ahead of time. It could just as well be...
a
b
c
d
or...
a b c d
e f g h
i j k l
m n o p
In my first example, I can see through observation that the possibilities are:
aabd
aabe
aabf
aacd
aace
aacf
babd
babe
babf
bacd
bace
bacf
cabd
cabe
cabf
cacd
cace
cacf
However, I just can't figure out the algorithm. Can anybody help me get my head around this? If I knew the sizes ahead of time, I could do it, but I don't. I feel like a recursive function is the answer, but I just can't see it.
EDIT: Here is the code I have so far to get the values into a matrix. In some cases, the value is stand alone, and in others, it comes in sets of multiple values, surrounded by parenthesis...
int CountTestCases(const string &iTestStr, const map<string::size_type, string::size_type> &iHashTable)
{
vector<vector<char>> charMatrix;
string::const_iterator strIt = iTestStr.begin();
while(strIt != iTestStr.end())
{
if(*strIt == '(')
{
++strIt;
char c = *strIt;
vector<char> tmpVec;
tmpVec.push_back(c);
++strIt;
while(*strIt != ')')
{
c = *strIt;
tmpVec.push_back(c);
++strIt;
}
charMatrix.push_back(tmpVec);
}
else
{
char c = *strIt;
vector<char> tmpVec;
tmpVec.push_back(c);
charMatrix.push_back(tmpVec);
}
++strIt;
}
return 0;
}