#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, m;
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
vector<string> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
bool visited[n][m];
memset(visited, 0, sizeof(visited));
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == '*' && !visited[i][j]) {
ans++;
queue<pair<int, int>> q;
q.push({i, j});
while (q.size()) {
auto cur = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nx = cur.first + dx[d], ny = cur.second + dy[d];
if (valid(nx, ny) && !visited[nx][ny] && v[nx][ny] == '*') {
visited[nx][ny] = 1;
q.push({nx, ny});
}
}
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |