#include <iostream>
#include <vector>
using namespace std;
void generatePermutations(vector<int> &perm, int start) {
if (start == perm.size()) { // Điều kiện dừng
for (int x : perm) cout << x << " ";
cout << endl;
return;
}
for (int i = start; i < perm.size(); i++) {
swap(perm[start], perm[i]); // Hoán đổi phần tử
generatePermutations(perm, start + 1); // Gọi đệ quy
swap(perm[start], perm[i]); // Backtrack (khôi phục trạng thái ban đầu)
}
}
int main() {
vector<int> perm = {1, 2, 3};
generatePermutations(perm, 0);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBnZW5lcmF0ZVBlcm11dGF0aW9ucyh2ZWN0b3I8aW50PiAmcGVybSwgaW50IHN0YXJ0KSB7CiAgICBpZiAoc3RhcnQgPT0gcGVybS5zaXplKCkpIHsgLy8gxJBp4buBdSBraeG7h24gZOG7q25nCiAgICAgICAgZm9yIChpbnQgeCA6IHBlcm0pIGNvdXQgPDwgeCA8PCAiICI7CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGZvciAoaW50IGkgPSBzdGFydDsgaSA8IHBlcm0uc2l6ZSgpOyBpKyspIHsKICAgICAgICBzd2FwKHBlcm1bc3RhcnRdLCBwZXJtW2ldKTsgLy8gSG/DoW4gxJHhu5VpIHBo4bqnbiB04butCiAgICAgICAgZ2VuZXJhdGVQZXJtdXRhdGlvbnMocGVybSwgc3RhcnQgKyAxKTsgLy8gR+G7jWkgxJHhu4cgcXV5CiAgICAgICAgc3dhcChwZXJtW3N0YXJ0XSwgcGVybVtpXSk7IC8vIEJhY2t0cmFjayAoa2jDtGkgcGjhu6VjIHRy4bqhbmcgdGjDoWkgYmFuIMSR4bqndSkKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICB2ZWN0b3I8aW50PiBwZXJtID0gezEsIDIsIDN9OwogICAgZ2VuZXJhdGVQZXJtdXRhdGlvbnMocGVybSwgMCk7CiAgICByZXR1cm4gMDsKfQ==