Advertisement
GastonFontenla

OIA N3P3A2010 - Recorriendo el laberinto

May 30th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <deque>
  4.  
  5. #define GB(m, x) ((m) &  (1LL<<(x)))
  6. #define SB(m, x) ((m) | (1LL<<(x)))
  7. #define CB(m, x) ((m) &= ~(1LL<<(x)))
  8.  
  9. using namespace std;
  10.  
  11. int x[4] = {-1, 1, 0, 0};
  12. int y[4] = {0, 0, -1, 1};
  13. char pa[4] = {'U', 'D', 'L', 'R'};
  14.  
  15. int d[4000][4000];
  16. int m[4000][4000];
  17. char p[4000][4000];
  18.  
  19. int bTam, aTam;
  20.  
  21. struct Par
  22. {
  23.     int primero;
  24.     int segundo;
  25. };
  26.  
  27. struct Grafo
  28. {
  29.     bool valido(int a, int b)
  30.     {
  31.         return (0 <= a && 0 <= b && a < bTam && b < aTam);
  32.     }
  33.  
  34.     void bfs(int a, int b)
  35.     {
  36.         deque <Par> cola;
  37.         d[a][b] = 0;
  38.         Par par;
  39.         par.primero = a;
  40.         par.segundo = b;
  41.         cola.push_back(par);
  42.         int e, f;
  43.  
  44.         int tamCola = 1;
  45.         while(tamCola)
  46.         {
  47.             a = cola.front().primero;
  48.             b = cola.front().segundo;
  49.             cola.pop_front();
  50.             tamCola--;
  51.  
  52.             for(int i=0; i<4; i++)
  53.             {
  54.                 e = a+x[i], f = b+y[i];
  55.                 if(valido(e, f)) ///Si es una posición válida
  56.                 {
  57.                     if(GB(m[a][b], i)) ///Si puedo ir en esa dirección
  58.                     {
  59.                         if(d[a][b]+1 < d[e][f]) ///Si mejoro la distancia
  60.                         {
  61.                             d[e][f] = d[a][b]+1;
  62.                             p[e][f] = pa[i];
  63.                             par.primero = e;
  64.                             par.segundo = f;
  65.                             cola.push_back(par);
  66.                             tamCola++;
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     void leer(istream &in, ostream &out)
  75.     {
  76.         int a, b, t, x1, y1, x2, y2;
  77.         in >> a >> b >> t;
  78.         bTam = b, aTam = a;
  79.  
  80.         for(int i=0; i<b; i++)
  81.             for(int j=0; j<a; j++)
  82.                 d[i][j] = 999999999, m[i][j] = 15, p[i][j] = ' ';
  83.  
  84.         for(int i=0; i<t; i++)
  85.         {
  86.             in >> x1 >> y1 >> x2 >> y2;
  87.  
  88.             /**
  89.             Los movimientos se describen:
  90.             Si puedo ir U, D, L, o R
  91.             0 bit U
  92.             1 bit D
  93.             2 bit L
  94.             3 bit R
  95.  
  96.             Si están off, no puedo ir.
  97.             **/
  98.  
  99.             /**
  100.             ¡Enunciado es garca!
  101.             Las coordenadas pueden estar en orden de der to izq
  102.             Los swap de abajo lo solucionan
  103.             **/
  104.  
  105.             if(y1 > y2)
  106.                 swap(y1, y2);
  107.             if(x1 > x2)
  108.                 swap(x1, x2);
  109.  
  110.             if(x1 == x2) ///Es vertical
  111.             {
  112.                 for(int j=y1; j<y2; j++)
  113.                 {
  114.                     m[j][x1-1] = CB(m[j][x1-1], 3);
  115.                     m[j][x1+0] = CB(m[j][x1+0], 2);
  116.                 }
  117.             }
  118.             else if(y1 == y2) ///Es horizontal
  119.             {
  120.                 for(int j=x1; j<x2; j++)
  121.                 {
  122.                     m[y1-1][j] = CB(m[y1-1][j], 1);
  123.                     m[y1+0][j] = CB(m[y1+0][j], 0);
  124.                 }
  125.             }
  126.         }
  127.         ///Listo, ya tengo todas las restricciones cargadas
  128.  
  129.         bfs(0, 0);
  130.  
  131.         if(d[b-1][a-1] == 999999999)
  132.             out << "imposible" << endl;
  133.         else
  134.             out << d[b-1][a-1] << endl;
  135.     }
  136. };
  137.  
  138. int main()
  139. {
  140.     ifstream in("tabiques.in");
  141.     ofstream out("tabiques.out");
  142.  
  143.     Grafo g;
  144.     g.leer(in, out);
  145.  
  146.     in.close();
  147.     out.close();
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement