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, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main(){
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i ++) cin >> a[i];
char cur = a[0][0];
int graph[n][m];
for (int i = 0; i < n; i ++){
for (int j = 0; j < m; j ++){
if (a[i][j] == cur) graph[i][j] = 1;
else if (a[i][j] == '.') graph[i][j] = 0;
else graph[i][j] = 2;
}
}
int ans = 1;
bool add = false;
for (int i = 0; i < n; i ++) for (int j = 0; j < m; j ++) if (graph[i][j] == 2) add = true;
if (add) ans ++;
add = false;
bool vis[n][m];
memset(vis, false, sizeof(vis));
vis[0][0] = true;
queue<pair<int, int> > que;
que.push(make_pair(0, 0));
while(! que.empty()){
pair<int, int> p = que.front();
que.pop();
for (int i = 0; i < 4; i ++){
int x = p.first + dx[i];
int y = p.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && ! vis[x][y]){
vis[x][y] = true;
que.push(make_pair(x, y));
}
}
}
for (int i = 0; i < n; i ++){
for (int j = 0; j < m; j ++){
if (graph[i][j] == 1 && ! vis[i][j]) {
add = true;
}
}
}
if (add) ans ++;
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |