This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main () {
int h, w;
cin >> h >> w;
char arr[h + 1][w + 1] = {};
for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> arr[i][j];
int ans = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (arr[i][j] != '.') {
queue <pair <int, int>> cur;
cur.push({i, j});
set <char> p;
while (!cur.empty()) {
auto k = cur.front();
cur.pop();
if (k.first < 1 || k.first > h) continue;
if (k.second < 1 || k.second > w) continue;
if (arr[k.first][k.second] == '.') continue;
p.insert(arr[k.first][k.second]);
arr[k.first][k.second] = '.';
for (int i = 0; i < 4; i++) {
cur.push({k.first + dx[i], k.second + dy[i]});
}
}
ans += p.size();
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |