GastonFontenla

Untitled

Jul 7th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. #define INF (1 << 29)
  8. #define Par pair<int, int>
  9. #define x first
  10. #define y second
  11.  
  12. int abs(int x)
  13. {
  14.     if(x < 0)
  15.         return -x;
  16.     return x;
  17. }
  18.  
  19. bool llego(const Par &a, const Par &b)
  20. {
  21.     int dx = abs(a.x - b.x);
  22.     int dy = abs(a.y - b.y);
  23.     int distMax = 50*50;
  24.     ///Hipotenusa al cuadrado es igual a la suma del cuadrado de los catetos
  25.     ///Con esto evito calcular flotantes
  26.     int hipotenusa = dx*dx + dy*dy;
  27.  
  28.     return (hipotenusa <= distMax);
  29. }
  30.  
  31. struct Grafo
  32. {
  33.     vector <vector <int> > adj;
  34.     vector <Par> listaPuntos;
  35.     vector <int> padre;
  36.  
  37.     void leer()
  38.     {
  39.         int a, b;
  40.         while(cin >> a >> b)
  41.             listaPuntos.push_back(make_pair(a, b));
  42.  
  43.         adj.resize(listaPuntos.size());
  44.         padre = vector <int> (listaPuntos.size(), -1);
  45.  
  46.         ///Inicialmente, ningun nodo tiene padre
  47.  
  48.         for(int i=0; i<listaPuntos.size(); i++)
  49.         {
  50.             for(int j=i+1; j<listaPuntos.size(); j++)
  51.             {
  52.                 if(llego(listaPuntos[i], listaPuntos[j]))
  53.                 {
  54.                     ///Añado esa arista entre los puntos
  55.                     adj[i].push_back(j);
  56.                     adj[j].push_back(i);
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     void BFS(int inicio)
  63.     {
  64.         vector <int> d(listaPuntos.size(), INF);
  65.         queue <int> cola;
  66.         d[inicio] = 0;
  67.         cola.push(inicio);
  68.  
  69.         while(cola.size())
  70.         {
  71.             int n = cola.front();
  72.             cola.pop();
  73.  
  74.             for(int i=0; i<adj[n].size(); i++)
  75.             {
  76.                 int vecino = adj[n][i];
  77.                 if(d[vecino] > d[n] + 1)
  78.                 {
  79.                     d[vecino] = d[n] + 1;
  80.                     padre[vecino] = n;
  81.                     cola.push(vecino);
  82.                 }
  83.             }
  84.         }
  85.  
  86.         ///Reconstruyo el camino
  87.  
  88.         vector <int> nodosSolucion;
  89.  
  90.         int nodo = listaPuntos.size()-1; ///El ultimo nodo
  91.  
  92.         if(listaPuntos.size() > 1 && padre[nodo] == -1)
  93.         {
  94.             ///Si en input hay mas de un nodo, y no pude llegar al nodo final
  95.             cout << "NO HAY RUTA." << endl;
  96.             return;
  97.         }
  98.  
  99.         nodosSolucion.push_back(nodo);
  100.         while(padre[nodo] != -1)
  101.         {
  102.             nodo = padre[nodo];
  103.             nodosSolucion.push_back(nodo);
  104.         }
  105.  
  106.         ///Ahora lo muestro al reves, o bien uso stack
  107.  
  108.         for(int i=nodosSolucion.size()-1; i>=0; i--)
  109.         {
  110.             cout << listaPuntos[nodosSolucion[i]].x << " ";
  111.             cout << listaPuntos[nodosSolucion[i]].y << endl;
  112.         }
  113.     }
  114. };
  115.  
  116. int main()
  117. {
  118.     Grafo g;
  119.     g.leer();
  120.     g.BFS(0);
  121.  
  122.     return 0;
  123. }
  124.  
  125. /**
  126. Input:
  127. 0 0
  128. -20 -30
  129. -50 0
  130. -30 30
  131. 40 0
  132. 80 0
  133. 120 0
  134. 160 0
  135. 40 30
  136. 90 30
  137. 60 50
  138. 120 70
  139. 160 50
  140. 40 80
  141. 160 100
  142.  
  143. **/
Advertisement
Add Comment
Please, Sign In to add comment