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
|
# include <iostream>
# include <vector>
# include <utility>
# include <algorithm>
constexpr auto ROW = 4;
constexpr auto COL = 4;
using vec2D = std::vector<std::vector<int>>;
vec2D transpose2D(vec2D& board)
{
vec2D transposeBoard{};
for (auto i = 0; i < COL; ++i)
{
size_t j{};
std::vector<int> inner{};
while (j < ROW)
{
inner.push_back(std::move(board[j][i]));
++j;
}
transposeBoard.push_back(std::move(inner));
inner.clear();
}
return transposeBoard;
}
int main()
{
std::vector<std::vector<int>> board { { 0, 0, 3, 0 }, { 0, 0, 0, 4 }, { 0, 3, 3, 0 }, { 0, 0, 0, 4 }};
auto firstTranspose = transpose2D(board);
for (auto & elem : firstTranspose)
{
std::stable_partition(elem.begin(), elem.end(), [](int i){return i != 0;});
}
auto transposeBack = transpose2D(firstTranspose);
}
|