Repository navigation
Expand file tree
/
Copy pathfloodfill.cpp
More file actions
105 lines (89 loc) · 1.88 KB
/
Copy pathfloodfill.cpp
File metadata and controls
105 lines (89 loc) · 1.88 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef vector<int> vi;
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
const int MOD = 1000000007;
double PI = 4*atan(1);
/*
// how to sort+ use the iterator:)
std::sort(v.begin(), v.end(), std::greater<int>());
std::vector<int>::iterator low, up;
low = std::lower_bound(v.begin(), v.end(), 3, std::greater<int>());
up = std::upper_bound(v.begin(), v.end(), 5, std::greater<int>());
*/
// #PREFIX SUM 1 index Based.
/*
bool comp(string s1,string s2){
// return the one smaller lexicographically
return s1+s2 < s2+s1;
}*/
const int maxN=1010;
char matrix[maxN][maxN];
bool visited[maxN][maxN];
int n,m;
bool validity(int x,int y){
if(x<0 || x>=n || y<0 || y>=m){
return false;
}
if(matrix[x][y]=='#'){
return false;
}
if(visited[x][y]){
return false;
}
return true;
}
void floodFill(int x,int y){
if(!validity(x,y)){
return;
}
visited[x][y]=true;
floodFill(x+1,y);
floodFill(x-1,y);
floodFill(x,y+1);
floodFill(x,y-1);
}
void solve(){
cin >> n >>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> matrix[i][j];
}
}
int counter=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!visited[i][j]&&matrix[i][j]=='.'){
counter++;
//cout << i << " " << j <<endl;
floodFill(i,j);
}
}
}
cout << counter << "\n";
}
int main()
{
// Use standard input to read from "paint.in"
//freopen("fenceplan.in", "r", stdin);
// Use standard output to write to "paint.out"
//freopen("fenceplan.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
//int tc;
//cin >> tc;
//while(tc--){
//while(scanf("%d %d",&m,&n)!=EOF)
solve();
//}
return 0;
}