fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, ski = 0;//ski dung de tinh so hang,n la de ....................................................................................................................................................................................................................................................................lam so cuoi
  5. vector<int> a; //in ra chuoi banana
  6. bool beo[12];
  7. vector<vector<int>> res;
  8.  
  9. void backTracking() {
  10. if (a.size() == n) {
  11. res.push_back(a);
  12. return;
  13. }
  14.  
  15. for (int i = 1; i <= n; i++) {
  16. if (beo[i] == false && (a.size() == 0 || a.size() > 0 && (i + a.back()) % 3 != 0)) {
  17. beo[i] = true;
  18. a.push_back(i);
  19. backTracking();
  20. a.pop_back();
  21. beo[i] = false;
  22. }
  23. }
  24. }
  25.  
  26. int main() {
  27. cin >> n;
  28.  
  29. backTracking();
  30. cout << res.size() << '\n';
  31. for (auto perm : res) {
  32. for (auto i : perm) {
  33. cout << i << " ";
  34. }
  35. cout << '\n';
  36. }
  37. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
1