GastonFontenla

Untitled

Oct 4th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  7. int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
  8.  
  9. vector<vector<bool> >v;
  10. vector <string> dep;
  11. int ancho,alto,ca=0;
  12. bool esValido(int x, int y) ///Si existe
  13. {
  14.     return (0 <= x && x < ancho && 0 <= y && y < alto);
  15. }
  16. void init(int width, int height)
  17. {
  18.     ancho = width;
  19.     alto = height;
  20.  
  21.     v = vector <vector <bool> > (alto, vector <bool> (ancho, false));
  22.     dep = vector <string> (alto);
  23. }
  24. void floodfill(int x, int y)
  25. {
  26.     v[x][y] = true;
  27.  
  28.  
  29.     for(int i=0; i<8; i++)
  30.     {
  31.         int a = x + dx[i];
  32.         int b = y + dy[i];
  33.  
  34.         if(esValido(b,a) && dep[a][b] == '@' && v[a][b] == false)
  35.             floodfill(a, b);
  36.  
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.     vector<int>res;
  43.     int f,c,m,n;
  44.     cin>>m>>n;
  45.     while(m!=0 || n!=0)
  46.     {
  47.         init(n,m);
  48.         for(f=0; f<m; f++)
  49.             cin>>dep[f];
  50.  
  51.  
  52.  
  53.  
  54.         for(f=0; f<m; f++)
  55.         {
  56.             for(c=0; c<n; c++)
  57.             {
  58.                 if((dep[f][c]=='@') && (v[f][c]==false))
  59.                 {
  60.                     ca++;
  61.                     floodfill(f,c);
  62.                 }
  63.  
  64.             }
  65.         }
  66.         ///variable que informa cuantas veces se uso el floodfill
  67.         res.push_back(ca);
  68.         ca=0;
  69.         cin>>m>>n;
  70.  
  71.  
  72.     }
  73.     int i;
  74.     for(i=0; i<res.size(); i++)
  75.         cout<<res[i]<<endl;
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment