GastonFontenla

N2P4 - Comodines

Sep 1st, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int dx[] = {1, 0, -1, 0};
  6. int dy[] = {0, 1, 0, -1};
  7. int alto, ancho, tamComp;
  8.  
  9. vector <vector <bool> > visit;
  10. vector <vector <int> > g;
  11.  
  12. bool esValido(int x, int y)
  13. {
  14.     return 0 <= x && x < alto && 0 <= y && y < ancho;
  15. }
  16.  
  17. void floodfill(int x, int y, int val)
  18. {
  19.     visit[x][y] = true;
  20.     tamComp++;
  21.  
  22.     for(int i=0; i<4; i++)
  23.     {
  24.         int x2 = x+dx[i];
  25.         int y2 = y+dy[i];
  26.  
  27.         if(esValido(x2, y2) && !visit[x2][y2])
  28.             if(g[x2][y2] == 0 || g[x2][y2] == val)
  29.                 floodfill(x2, y2, val);
  30.     }
  31. }
  32.  
  33. int maxComponente(int val)
  34. {
  35.     visit = vector <vector <bool> > (alto, vector <bool> (ancho, false));
  36.  
  37.     int maxComp = 0;
  38.     for(int i=0; i<g.size(); i++)
  39.     {
  40.         for(int j=0; j<g.size(); j++)
  41.         {
  42.             if(g[i][j] == 0 || g[i][j] == val)
  43.             {
  44.                 if(!visit[i][j])
  45.                 {
  46.                     tamComp = 0;
  47.                     floodfill(i, j, val);
  48.                     maxComp = max(maxComp, tamComp);
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     return maxComp;
  55. }
  56.  
  57. int comodines(vector <vector <int> > grilla)
  58. {
  59.     g = grilla;
  60.     alto = g.size();
  61.     ancho = g[0].size();
  62.  
  63.     int maxRes = 0;
  64.     for(int i=1; i<=1000; i++)
  65.         maxRes = max(maxRes, maxComponente(i));
  66.  
  67.     return maxRes;
  68. }
  69.  
  70. /**
  71. //Función main auxiliar para testear
  72. int main()
  73. {
  74.     int N, M;
  75.     cin >> N >> M;
  76.     vector <vector <int> > grilla(N, vector <int> (M));
  77.  
  78.     for(int i=0; i<N; i++)
  79.         for(int j=0; j<M; j++)
  80.             cin >> grilla[i][j];
  81.  
  82.     cout << comodines(grilla) << endl;
  83.  
  84.     return 0;
  85. }
  86. **/
Advertisement
Add Comment
Please, Sign In to add comment