-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlood Fill make largest island.cpp
More file actions
81 lines (66 loc) · 1.41 KB
/
Flood Fill make largest island.cpp
File metadata and controls
81 lines (66 loc) · 1.41 KB
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
#include<bits/stdc++.h>
using namespace std;
const int N = 100;
int a[N][N], vis[N][N];
int col_cnt[N];
int n, m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void flood_fill(int x, int y, int col) {
a[x][y] = col;
col_cnt[col]++;
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && vis[xx][yy] == 0 && a[xx][yy] == 1) {
flood_fill(xx, yy, col);
}
}
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
int total_count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 1 && vis[i][j] == 0) {
total_count++;
flood_fill(i, j, total_count);
}
}
}
int largest_island = 0;
for (int i = 1; i <= total_count; i++) {
largest_island = max(largest_island, col_cnt[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 0) {
// can be converted
set<int> St;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k];
int jj = j + dy[k];
if (ii >= 0 && ii < n && jj >= 0 && jj < m) {
St.insert(a[ii][jj]);
}
}
int ans = 1;
for (auto x : St) {
ans += col_cnt[x];
}
largest_island = max(largest_island, ans);
}
}
}
cout << largest_island;
return 0;
}