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>
#define endl "\n"
#define ll long long
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define gett(tup,ind) get<ind>(tup)
#define print(x) for (auto i:x){cout<<i<<" ";}cout<<endl;
using namespace std;
int h,w;
char arr[4000][4000];
bool vis[4000][4000];
void fill(int x,int y){
queue <pair<int,int>> bfs;bfs.emplace(x,y);
while (bfs.size()){
auto [a,b]=bfs.front();bfs.pop();
if (vis[a][b])continue;
vis[a][b]=true;
if (a+1<h && arr[a][b]==arr[a+1][b]){bfs.emplace(a+1,b);}
if (b+1<w && arr[a][b]==arr[a][b+1]){bfs.emplace(a,b+1);}
if (a>0 && arr[a][b]==arr[a-1][b]){bfs.emplace(a-1,b);}
if (b>0 && arr[a][b]==arr[a][b-1]){bfs.emplace(a,b-1);}
}
}
void solve(){
cin>>h>>w;
set <char> st;
for (int i=0;i<h;i++){
for (int j=0;j<w;j++){
cin>>arr[i][j];st.emplace(arr[i][j]);
}
}
char lst=arr[0][0];
int ans=st.size()-st.count('.');
queue <pair<int,int>> bfs;bfs.emplace(0,0);
while (bfs.size()){
auto [a,b]=bfs.front();bfs.pop();
if (vis[a][b])continue;
vis[a][b]=true;
if (a+1<h && ((arr[a][b]==arr[a+1][b]) || (arr[a][b]==lst && arr[a+1][b]!='.'))){bfs.emplace(a+1,b);}
if (b+1<w && ((arr[a][b]==arr[a][b+1]) || (arr[a][b]==lst && arr[a][b+1]!='.'))){bfs.emplace(a,b+1);}
if (a>0 && ((arr[a][b]==arr[a-1][b]) || (arr[a][b]==lst && arr[a-1][b]!='.'))){bfs.emplace(a-1,b);}
if (b>0 && ((arr[a][b]==arr[a][b-1]) || (arr[a][b]==lst && arr[a][b-1]!='.'))){bfs.emplace(a,b-1);}
}
for (int i=0;i<h;i++){
for (int j=0;j<w;j++){
if (vis[i][j] || arr[i][j]=='.')continue;
fill(i,j);ans++;
}
}
cout<<ans<<endl;
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int no_test=1;
//cin>>no_test;
while (no_test--){solve();}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |