SHARE
TWEET

Untitled

a guest Dec 8th, 2016 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QDebug>
  2.  
  3. #include <QVector>
  4. #include <QMap>
  5. #include <QQueue>
  6. #include <limits>
  7. //-----------------------------------------------------------------------------
  8. //Поиск родителя
  9. QMap<int, int>::ConstIterator findParent(const QMap<int, int> &map,
  10.                                          int child){
  11.     QMap<int, int>::ConstIterator parent = map.find(child);
  12.  
  13.     if(child == *parent){
  14.         return map.end();
  15.     }
  16.  
  17.     return parent;
  18. }
  19. //-----------------------------------------------------------------------------
  20. //Построение маршрута
  21. QVector<int> route(const QMap<int, int> &map,
  22.                    int start){
  23.     QVector<int> route;
  24.     route.append(start);
  25.  
  26.     forever{
  27.         QMap<int, int>::ConstIterator parent = findParent(map, route.last());
  28.  
  29.         if(parent == map.end()){
  30.             break;
  31.         }
  32.  
  33.         route.append(*parent);
  34.     };
  35.  
  36.     return route;
  37. }
  38. //------------------------------------------------------------------------------
  39. //Построение маршрутов
  40. QVector<QVector<int> > routes(const QMap<int, int> &map){
  41.     QVector<QVector<int> > routes;
  42.     QMap<int, int>::ConstIterator begin = map.begin();
  43.     QMap<int, int>::ConstIterator end = map.end();
  44.  
  45.     for(; begin != end; ++begin){
  46.         routes.append(route(map, begin.key()));
  47.     }
  48.  
  49.     return routes;
  50. }
  51. //------------------------------------------------------------------------------
  52. int main(int, char *[]){
  53.  
  54.     QMap<int, int> m;
  55.     m[0] = 0;
  56.     m[1] = 0;
  57.     m[2] = 3;
  58.     m[3] = 1;
  59.     m[4] = 1;
  60.  
  61.     qDebug() << routes(m);
  62.  
  63.     return 0;
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top