Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. int N;
  6. int V[10];
  7. int M[10];
  8.  
  9. ifstream i("permutari1.in");
  10. ofstream o("permutari1.out");
  11.  
  12.  
  13. void backtrack(int x)
  14. {
  15. if(x == N)
  16. {
  17. for(int a = 0; a < x; a++)
  18. {
  19. o << M[a] << " ";
  20. }
  21.  
  22. o << '\n';
  23. return;
  24. }
  25.  
  26. else
  27. {
  28. for(int a = N; a >= 1; a--)
  29. {
  30. if(V[a] != -1)
  31. {
  32. int bup = V[a];
  33. M[x] = V[a];
  34. V[a] = -1;
  35.  
  36. backtrack(x + 1);
  37.  
  38. V[a] = bup;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. i >> N;
  47.  
  48. for(int a = 0; a <= N; a++)
  49. {
  50. V[a] = a;
  51. }
  52.  
  53. backtrack(0);
  54.  
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement