Here's an outline of the steps for the permuatation of the rows:
std::vector<std::vector<int>> - the 2D matrix that has the given numbers
extract row and col information from the matrix
initialize vector 0, 1, ..., row size - 1 with std::iota to run permutations of the number of rows:
http://en.cppreference.com/w/cpp/algorithm/iota
run std::next_permutation on this vector
http://en.cppreference.com/w/cpp/algorithm/next_permutation
declare another std::vector<std::vector<int> - capture each permuation order and push_back into this 2D vector
finally one last 3D std::vector<std::vector<std::vector<int>>> that will have the various row permuation vectors
in the program below i've tried to comment within the program as well on the above lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <algorithm> // std::next_permutation, std::iota
#include <vector>
int main ()
{
std::vector<std::vector<int>> myVec{{1,2,3, 8}, {2,3,1,5},{3,1,2,2}};//given 2D matrix
std::vector<int> rowNum(myVec.size());//extract row information
std::iota(rowNum.begin(), rowNum.end(), 0);//to capture the permuation of the number of rows ...
//http:en.cppreference.com/w/cpp/algorithm/iota
std::vector<std::vector<int>> rowCombos{};//... and the actual permutations are saved in this vector
do
{
{
std::vector<int> tempVec{};
for (size_t i = 0; i < myVec.size(); ++i)
{
tempVec.push_back(rowNum[i]);
}
rowCombos.push_back(tempVec);
tempVec.clear();//http://en.cppreference.com/w/cpp/container/vector/clear
}
} while ( std::next_permutation(rowNum.begin(),rowNum.end()));
//http://en.cppreference.com/w/cpp/algorithm/next_permutation
std::vector<std::vector<std::vector<int>>> threeDVec{};//the 3D vector that'll save the verious permuations by row of the 2d vector
for (const auto& elem : rowCombos)
{
std::vector<std::vector<int>> tempVec{};//a temporary holding vector of the various 2d vector permutations until push_back into the 3D vector
for (const auto& elemI : elem)
{
tempVec.push_back(myVec[elemI]);
}
threeDVec.push_back(tempVec);
tempVec.clear();
}
for (const auto& elem1 : threeDVec)//now print out the 3D vector
{
for (const auto& elem2: elem1)
{
for (const auto& elem3 : elem2)
{
std::cout << elem3 << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
}
/* output
1 2 3 8
2 3 1 5
3 1 2 2
1 2 3 8
3 1 2 2
2 3 1 5
2 3 1 5
1 2 3 8
3 1 2 2
2 3 1 5
3 1 2 2
1 2 3 8
3 1 2 2
1 2 3 8
2 3 1 5
3 1 2 2
2 3 1 5
1 2 3 8
*/
|
OK, so now the question remains how are we going to swap the cols? well I think one way might be to transpose the vector first so that the cols become rows and then apply the above program and transpose the vectors back! and a good way to transpose a 2D vector can be found here:
http://stackoverflow.com/questions/6009782/how-to-pivot-a-vector-of-vectors, particularly the second answer from user Bjorn Pollex
edit: if
I'm wondering how can it be that
? shouldn't it be 240?