#include <algorithm>
#include <array>
#include <iostream>
using namespace std;
#define WIDTH 3
#define HEIGHT 2
int main() {
const array<array<int, HEIGHT>, WIDTH> pCells = { 1, 2, 11, 12, 21, 22 };
int lCells[WIDTH * HEIGHT];
for_each(begin(lCells), end(lCells), [i = cbegin(pCells), j = cbegin(pCells)->cbegin()](auto& it) mutable {
if(j == i->cend()) j = (++i)->cbegin();
it = *j++;
});
for(const auto& i : lCells) {
cout << i << ' ';
}
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGFycmF5PgojaW5jbHVkZSA8aW9zdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBXSURUSCAzCiNkZWZpbmUgSEVJR0hUIDIKCmludCBtYWluKCkgewoJY29uc3QgYXJyYXk8YXJyYXk8aW50LCBIRUlHSFQ+LCBXSURUSD4gcENlbGxzID0geyAxLCAyLCAxMSwgMTIsIDIxLCAyMiB9OwoJaW50IGxDZWxsc1tXSURUSCAqIEhFSUdIVF07CgkKCWZvcl9lYWNoKGJlZ2luKGxDZWxscyksIGVuZChsQ2VsbHMpLCBbaSA9IGNiZWdpbihwQ2VsbHMpLCBqID0gY2JlZ2luKHBDZWxscyktPmNiZWdpbigpXShhdXRvJiBpdCkgbXV0YWJsZSB7CgkJaWYoaiA9PSBpLT5jZW5kKCkpIGogPSAoKytpKS0+Y2JlZ2luKCk7CgkJCgkJaXQgPSAqaisrOwoJfSk7CgkKCWZvcihjb25zdCBhdXRvJiBpIDogbENlbGxzKSB7CgkJY291dCA8PCBpIDw8ICcgJzsKCX0KfQ==