#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
void PlayGround() {
int n, m; cin >> n >> m;
vector<string>g(n);
for(string& row : g) cin >> row;
int ans = 0;
auto BFS = [&] (int i, int j) {
queue<pair<int,int>>q;
q.push({i, j});
auto valid = [=] (int x, int y) {
return x>-1 && y>-1 && x<n && y<m && g[x][y]=='*';
};
while(!q.empty()) {
pair<int,int>cur = q.front(); q.pop();
if(g[cur.first][cur.second]!='*') continue;
g[cur.first][cur.second] = '.';
int dx[] {1, -1, 0, 0};
int dy[] {0, 0, 1, -1};
for(int k=0; k<4; ++k) {
int new_i = cur.first + dx[k], new_j = cur.second + dy[k];
if(valid(new_i, new_j))
q.push({new_i, new_j});
}
}
};
for(int i=0; i<n; ++i) {
for(int j=0; j<m; ++j) {
if(g[i][j]=='*') {
BFS(i, j), ++ans;
}
}
}
cout << ans << '\n';
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// #endif
PlayGround();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
236 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |