AlenAntonelli

Floodfill (DIJKSTRA)

Jun 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #define INF (1<<29)
  5. using namespace std;
  6.  
  7. int dx[] = {0, 0, 1,-1};
  8. int dy[] = {1,-1, 0, 0};
  9.  
  10. struct grafo {
  11.     vector< string > ady;
  12.     vector<vector<int>> dist;
  13.     priority_queue< pair<int, pair<int,int> > > pq;
  14.     int n, m, p;
  15.    
  16.     void leer ()
  17.     {
  18.         cin>>n>>m;
  19.        
  20.         ady.resize(n);
  21.         dist = vector< vector<int> > (n, vector<int> (m, INF) );
  22.        
  23.         for(int x=0; x<n; x++)
  24.             for(int y=0; y<m; y++)
  25.                 cin>>ady[x][y];
  26.         cin>>p;
  27.     }
  28.    
  29.     bool visitable (int x, int y)
  30.     {
  31.         if ((0<=x && x<n) && (0<=y && y<m))
  32.             if(ady[x][y]!='#')
  33.                 return true;
  34.         return false;
  35.     }
  36.    
  37.     void iniciar (int x, int y)
  38.     {
  39.         if ( visitable( x,y ) )
  40.         {
  41.             dist[x][y] = ady[x][y]-'0';
  42.             pq.push( {ady[x][y], {x,y} } );
  43.         }
  44.     }
  45.    
  46.     void dijkstra ()
  47.     {
  48.         for(int x=0; x<n; x++)
  49.         {
  50.             iniciar(x, 0  );
  51.             iniciar(x, m-1);
  52.         }
  53.         for(int y=0; y<m; y++)
  54.         {
  55.             iniciar(0  , y);
  56.             iniciar(n-1, y);
  57.         }
  58.        
  59.         while( pq.size() )
  60.         {
  61.             int x = pq.top().second.first;
  62.             int y = pq.top().second.second;
  63.             pq.pop();
  64.            
  65.             for(int i=0; i<4; i++)
  66.             {
  67.                 int vx = x+dx[i];
  68.                 int vy = y+dy[i];
  69.                 int costo = ady[vx][vy]-'0';
  70.                
  71.                 if ( visitable(vx,vy) )
  72.                 {
  73.                     if ( dist[x][y]+costo < dist[vx][vy] )
  74.                     {
  75.                         dist[vx][vy] = dist[x][y]+costo;
  76.                         pq.push( {-dist[vx][vy], {vx,vy} } );
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.        
  82.         /**for(int x=0; x<n; x++)
  83.         {
  84.             for(int y=0; y<m; y++)
  85.                 cout<<ady[x][y]<<"  ";
  86.             cout<<endl;
  87.         }
  88.         cout<<endl;*/
  89.        
  90.         for(int x=0; x<n; x++)
  91.         {
  92.             for(int y=0; y<m; y++)
  93.             {
  94.                 if (ady[x][y]=='#')
  95.                     cout<<"#  ";
  96.                 else if (dist[x][y]==INF)
  97.                     cout<<"N0 ";
  98.                 else if (dist[x][y]<10)
  99.                     cout<<dist[x][y]<<"  ";
  100.                 else cout<<dist[x][y]<<" ";
  101.             }
  102.             cout<<endl;
  103.         }
  104.         cout<<endl;
  105.        
  106.         int x, y;
  107.         for(int i=0; i<p; i++)
  108.         {
  109.             cin>>x>>y;
  110.             cout<<x<<" "<<y<<" -> ";
  111.             if(dist[x-1][y-1]==INF)
  112.                 cout<<"NO"<<endl;
  113.             else cout<<dist[x-1][y-1]<<endl;
  114.         }
  115.     }
  116. };
  117.  
  118. int main()
  119. {
  120.     grafo g;
  121.     g.leer();
  122.     g.dijkstra();
  123.  
  124.     return 0;
  125. }
  126.  
  127. /*
  128.  
  129. 8 15
  130. 0 # # # # # # # # # # # # # #
  131. 1 1 6 8 8 8 8 # 1 8 8 8 2 2 #
  132. # 1 # # # # # # 1 # # # # 2 #
  133. # 1 2 2 2 2 0 8 1 2 2 2 2 2 #
  134. # 1 # # # # 0 # 1 # # # # # #
  135. # 1 # # 5 # 0 1 1 # 9 9 # 1 #
  136. # 3 3 # 5 # # # # # 9 # # 1 #
  137. # # # # 5 # # # # # 9 # # # #
  138. 4
  139. 7 3
  140. 4 8
  141. 2 13
  142. 7 13
  143.  
  144. */
Advertisement
Add Comment
Please, Sign In to add comment