#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
const int N = 10001;
vi dad(N);
int find(int x) {
if (x == dad[x]) return x;
return dad[x] = find(dad[x]);
}
void unite(int x,int y) {
dad[find(x)] = find(y);
}
void solve() {
int n,m;
cin >> n >> m;
char grid[n+1][m+1];
F(i,n){
F(j,m) cin >> grid[i][j];
}
F(i,n*m) dad[i] = i;
for (int i=1;i<=n;i++) {
F(j,m) {
if (grid[i][j] == '*') {
if (i<n && grid[i+1][j] == '*') unite((i-1)*m+j,i*m+j);
if (j<m && grid[i][j+1] == '*') unite((i-1)*m+j,(i-1)*m+j+1);
}
}
}
set<int> s;
F(i,n) F(j,m) if (grid[i][j] == '*') s.insert(find((i-1)*m+j));
cout << s.size() << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
532 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
540 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |