Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int dx[] = {1, 0, -1, 0};
- int dy[] = {0, 1, 0, -1};
- int alto, ancho, tamComp;
- vector <vector <bool> > visit;
- vector <vector <int> > g;
- bool esValido(int x, int y)
- {
- return 0 <= x && x < alto && 0 <= y && y < ancho;
- }
- void floodfill(int x, int y, int val)
- {
- visit[x][y] = true;
- tamComp++;
- for(int i=0; i<4; i++)
- {
- int x2 = x+dx[i];
- int y2 = y+dy[i];
- if(esValido(x2, y2) && !visit[x2][y2])
- if(g[x2][y2] == 0 || g[x2][y2] == val)
- floodfill(x2, y2, val);
- }
- }
- int maxComponente(int val)
- {
- visit = vector <vector <bool> > (alto, vector <bool> (ancho, false));
- int maxComp = 0;
- for(int i=0; i<g.size(); i++)
- {
- for(int j=0; j<g.size(); j++)
- {
- if(g[i][j] == 0 || g[i][j] == val)
- {
- if(!visit[i][j])
- {
- tamComp = 0;
- floodfill(i, j, val);
- maxComp = max(maxComp, tamComp);
- }
- }
- }
- }
- return maxComp;
- }
- int comodines(vector <vector <int> > grilla)
- {
- g = grilla;
- alto = g.size();
- ancho = g[0].size();
- int maxRes = 0;
- for(int i=1; i<=1000; i++)
- maxRes = max(maxRes, maxComponente(i));
- return maxRes;
- }
- /**
- //Función main auxiliar para testear
- int main()
- {
- int N, M;
- cin >> N >> M;
- vector <vector <int> > grilla(N, vector <int> (M));
- for(int i=0; i<N; i++)
- for(int j=0; j<M; j++)
- cin >> grilla[i][j];
- cout << comodines(grilla) << endl;
- return 0;
- }
- **/
Advertisement
Add Comment
Please, Sign In to add comment