-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connected Cell in a Grid.cpp
91 lines (84 loc) · 2.46 KB
/
Connected Cell in a Grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
using namespace std;
int a[200][200];
vector< vector< int > >v;
vector< int >visit;
int mark[2000];
int comp=0;
void dfs(int s){
visit[s]=1;
mark[s]=comp;
for(vector< int >::iterator i=v[s].begin();i!=v[s].end();i++){
if(!visit[*i]){
//comp++;
dfs(*i);}
}
}
int pos[200][200];
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int row, col;
cin>>row>>col;
int k=1;
for(int u=1;u<=row;u++){
for(int j=1;j<=col;j++){ cin>>a[u][j];pos[u][j]=k;k++;}
}
v=vector< vector< int > >(k+1);
for(int u=1;u<=row;u++){
for(int j=1;j<=col;j++){
if(a[u][j]==1){
int poss=pos[u][j];
if(a[u-1][j]==1){
v[poss].push_back(pos[u-1][j]);
v[pos[u-1][j]].push_back(poss);
}
if(a[u+1][j]==1){
v[poss].push_back(pos[u+1][j]);
v[pos[u+1][j]].push_back(poss);
}
if(a[u][j-1]==1){
v[poss].push_back(pos[u][j-1]);
v[pos[u][j-1]].push_back(poss);
}
if(a[u][j+1]==1){
v[poss].push_back(pos[u][j+1]);
v[pos[u][j+1]].push_back(poss);
}
if(a[u-1][j+1]==1){
v[poss].push_back(pos[u-1][j+1]);
v[pos[u-1][j+1]].push_back(poss);
}
if(a[u-1][j-1]==1){
v[poss].push_back(pos[u-1][j-1]);
v[pos[u-1][j-1]].push_back(poss);
}
if(a[u+1][j+1]==1){
v[poss].push_back(pos[u+1][j+1]);
v[pos[u+1][j+1]].push_back(poss);
}
if(a[u+1][j-1]==1){
v[poss].push_back(pos[u+1][j-1]);
v[pos[u+1][j-1]].push_back(poss);
}
}
}
}
visit=vector< int >(k+1,0);
int ans=0;
for(int jj=1;jj<=k;jj++){
if(!visit[jj]){
// comp=1;
dfs(jj);
comp++;
// ans= max(ans, comp);
}
}
map< int , int >mm;
for(int jj=1;jj<=k;jj++){
mm[mark[jj]]++;
ans= max(ans, mm[mark[jj]]);
// cout<< mark[jj]<<" ";
}
cout<< ans<<endl;
return 0;
}