Advertisement
GastonFontenla

Untitled

Jun 19th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int dx[] = {0, 0, -1, 1}; ///Esto se declara
  7. int dy[] = {-1, 1, 0, 0}; ///siempre global
  8.  
  9. struct Grafo
  10. {
  11.     vector <string> m;
  12.     vector <vector <bool> > v; ///matriz de visitados
  13.     int alto, ancho;
  14.  
  15.     bool esValido(int i, int j)
  16.     {
  17.         return (0 <= i && i < alto+2 && 0 <= j && j < ancho+2);
  18.     }
  19.  
  20.     void floodfill(int i, int j)
  21.     {
  22.         v[i][j] = true;
  23.  
  24.         ///GUARDA CON EL RUNTIME ERROR!!!
  25.  
  26.         for(int k=0; k<4; k++)
  27.         {
  28.             int i2 = i+dy[k];
  29.             int j2 = j+dx[k];
  30.  
  31.             if(esValido(i2, j2) && m[i2][j2] == '_' && !v[i2][j2]) ///Si puedo recorrer la celda de arriba
  32.             {
  33.                 floodfill(i2, j2);
  34.             }
  35.         }
  36.     }
  37.  
  38.     void leer()
  39.     {
  40.         cin >> alto >> ancho;
  41.  
  42.         m = vector <string> (alto+2); ///Matriz con input
  43.         v = vector <vector <bool> > (alto+2, vector <bool> (ancho+2, false)); ///Matriz de visitados
  44.  
  45.         m[0] = string(ancho+2, '_'); ///Constructor del string
  46.         m[alto+1] = string(ancho+2, '_'); ///Constructor del string
  47.  
  48.         for(int i=1; i<alto+1; i++)
  49.         {
  50.             cin >> m[i];
  51.             m[i] = "_" + m[i] + "_";
  52.         }
  53.  
  54.         floodfill(0, 0);
  55.  
  56.  
  57.  
  58.         ///Responder las consultas
  59.     }
  60.  
  61. };
  62.  
  63. int main()
  64. {
  65.     Grafo g;
  66.     g.leer();
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement