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;
using ll = long long;
const int mxN = 4e3+10;
char matrix[mxN][mxN];
bool vis[mxN][mxN];
int n, m;
vector<int> xy = {1, 0, -1, 0}, yx = {0, 1, 0, -1};
int add = 1;
void dfs(int x, int y) {
char c = matrix[x][y];
vis[x][y] = true;
for(int i = 0; i < 4; i++) {
int a = x + xy[i], b = y + yx[i];
if(a >= 0 && a < n && b >= 0 && b < m && !vis[a][b]) {
if(matrix[a][b] == c){
dfs(a, b);
}
if(matrix[a][b] != c && matrix[a][b] != '.') {
add = 2;
dfs(a, b);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(matrix[i][j] != '.' && !vis[i][j]) {
dfs(i, j);
ans += add;
add = 1;
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |