fork download
  1. // Mohib Manva.
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. vector<int> vc;
  6. int n;
  7. int S = 16;
  8.  
  9. void solve(int ind) {
  10. if (ind == n) {
  11. vector<int> tmp;
  12. for (int x : vc) {
  13. tmp.push_back(x);
  14. }
  15. sort(tmp.begin(), tmp.end());
  16. bool ok = true;
  17. int sum = 0;
  18. for (int i = 0; i < n;i ++) {
  19. ok &= (tmp[i] == (i + 1));
  20. if (i % 2) {
  21. sum += abs(vc[i] - vc[i-1]);
  22. }
  23. }
  24. // for (int x : vc) {
  25. // printf("%d ", x);
  26. // }
  27. // puts("");
  28. if (ok && sum == S) {
  29. for (int x : vc) {
  30. printf("%d ", x);
  31. }
  32. puts("");
  33. }
  34. return;
  35. }
  36.  
  37. if (vc[ind] != 0) {
  38. solve(ind+1);
  39. } else {
  40. for (int i = 1; i <= n;i++) {
  41. vc[ind] = i;
  42. solve(ind + 1);
  43. vc[ind] = 0;
  44. }
  45. }
  46. }
  47.  
  48. int main() {
  49. int t = 1;
  50. // scanf("%d",&t);
  51. while (t--) {
  52. scanf("%d", &n);
  53. for (int i = 0; i < n; i++) {
  54. int x;
  55. scanf("%d", &x);
  56. vc.push_back(x);
  57. }
  58.  
  59. solve(0);
  60.  
  61. }
  62. return 0;
  63. }
Success #stdin #stdout 0.01s 5272KB
stdin
8
3 0 0 0 5 0 8 0
stdout
3 6 1 7 5 2 8 4 
3 6 1 7 5 4 8 2 
3 6 2 7 5 1 8 4 
3 6 2 7 5 4 8 1 
3 6 4 7 5 1 8 2 
3 6 4 7 5 2 8 1 
3 6 7 1 5 2 8 4 
3 6 7 1 5 4 8 2 
3 6 7 2 5 1 8 4 
3 6 7 2 5 4 8 1 
3 6 7 4 5 1 8 2 
3 6 7 4 5 2 8 1 
3 7 1 6 5 2 8 4 
3 7 1 6 5 4 8 2 
3 7 2 6 5 1 8 4 
3 7 2 6 5 4 8 1 
3 7 4 6 5 1 8 2 
3 7 4 6 5 2 8 1 
3 7 6 1 5 2 8 4 
3 7 6 1 5 4 8 2 
3 7 6 2 5 1 8 4 
3 7 6 2 5 4 8 1 
3 7 6 4 5 1 8 2 
3 7 6 4 5 2 8 1