fork(1) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <ostream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. using HanoiPin = vector<unsigned>;
  9.  
  10. template<class PostAction>
  11. void move_hanoi
  12. (
  13. HanoiPin &from, HanoiPin &to, HanoiPin &temp,
  14. size_t count, PostAction post
  15. )
  16. {
  17. if(count>1)
  18. {
  19. move_hanoi(from, temp, to, count-1, post);
  20. move_hanoi(from, to, temp, 1, post);
  21. move_hanoi(temp, to, from, count-1, post);
  22. }
  23. else
  24. {
  25. to.push_back(from.back());
  26. from.pop_back();
  27. post();
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. auto count=1000000;
  34. HanoiPin a,b,c;
  35. generate_n(back_inserter(a), count, [=]() mutable
  36. {
  37. return count--;
  38. });
  39. auto print_hanoi=[](HanoiPin &h)
  40. {
  41. cout << "|";
  42. for(auto x : h)
  43. cout << x << "-";
  44. cout << endl;
  45. };
  46. auto printer=[&]
  47. {
  48. //print_hanoi(a);
  49. //print_hanoi(b);
  50. //print_hanoi(c);
  51. static auto cnt = 0;
  52. cout << "Move #" << (cnt++) << endl;
  53. };
  54. printer();
  55. move_hanoi(a,b,c,a.size(),printer);
  56. }
  57.  
Runtime error #stdin #stdout 0.01s 15192KB
stdin
Standard input is empty
stdout
Move #0