Advertisement
Guest User

swapsort

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include"function.h"
  2.  
  3. set<State> SwapSort::extend(State s)
  4. {
  5.     set<State> SS;
  6.     State t1 = s;
  7.     State t2 = s;
  8.  
  9.     int tmp;
  10.     tmp = t1[0];
  11.     t1[0] = t1[1];
  12.     t1[1] = tmp;
  13.     tmp = t2[0];
  14.     t2[0] = t2[t2.size()-1];
  15.     t2[t2.size()-1] = tmp;
  16.     SS.insert(t1);
  17.     SS.insert(t2);
  18.     return SS;
  19. }
  20.  
  21. void SwapSort::solve(int steps)
  22. {
  23.     while (steps>0) {
  24.         set<list<State>> oldPaths;
  25.         set<list<State>> newPaths;
  26.  
  27.         for (auto p : _paths) {
  28.             _explored.insert(p.back());
  29.             auto SS = extend(p.back());
  30.             for (auto s : SS) {
  31.                 auto search = _explored.find(s);
  32.                 if (search == _explored.end()) {
  33.                     auto np = p;
  34.                     np.push_back(s);
  35.                     if (is_sorted(s.begin(), s.end())) {
  36.                         _solutions.insert(np);
  37.                     } else {
  38.                         newPaths.insert(np);
  39.                     }
  40.                 }
  41.             }
  42.             oldPaths.insert(p);
  43.         }
  44.         for (auto p : oldPaths) {
  45.             _paths.erase(p);
  46.         }
  47.         for (auto p : newPaths) {
  48.             _paths.insert(p);
  49.         }
  50.         --steps;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement