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 n, m;
char a[4005][4005];
char last;
const int dx[4]={0, 0, 1, -1};
const int dy[4]={1, -1, 0, 0};
bool visited[4005][4005];
int ans=0;
bool check(int i, int j){
if (1<=i && i<=n && 1<=j && j<=m) return true;
else return false;
}
void floodfill(int i, int j){
if (check(i, j)==false || visited[i][j]) return;
visited[i][j]=true;
for (int t=0; t<4; t++){
int u=i+dx[t];
int v=j+dy[t];
floodfill(u, v);
}
}
int main(){
cin>>n>>m;
for (int i=1;i <=n; i++){
for (int j=1; j<=m; j++){
cin>>a[i][j];
if (i==1 && j==1){
last=a[i][j];
}
if (a[i][j]!=last && a[i][j]!='.'){
ans=1;
}
}
}
for (int i=1;i <=n; i++){
for (int j=1; j<=m; j++){
if (a[i][j]==last && visited[i][j]==false){
ans++;
floodfill(i, j);
}
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |