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;
#define fastinput cin.tie(nullptr)->sync_with_stdio(false);
#define endl '\n'
#define ll long long
const ll mOd=1e9+7;
const int mAx=INT_MAX;
const int nUm=1e3+3;
int n,m,k=0;
int a[22][22],b[22][22],c[22][22];
void dfs(int i, int j){
if(i<0 || i>=n || j<0 || j>=m || c[i][j])return;
c[i][j]=k;
// cout<<i<<" "<<j<<".. "<<a[i][j]<<" "<<a[i+1][j]<<" "<<b[i][j]<<" "<<b[i][j+1]<<endl;
if(a[i][j]==0) dfs(i-1,j);
if(a[i+1][j]==0) dfs(i+1,j);
if(b[i][j]==0) dfs(i,j-1);
if(b[i][j+1]==0) dfs(i,j+1);
}
int main()
{
fastinput;
cin>>n>>m;
for(int i=0 ; i<n+1 ; i++){
string s; cin>>s;
for(int j=0 ; j<m ; j++){
a[i][j]=s[j]-'0';
}
}
for(int i=0 ; i<n ; i++){
string s; cin>>s;
for(int j=0 ; j<m+1 ; j++){
b[i][j]=s[j]-'0';
}
}
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<m ; j++){
if(a[i][j] && a[i+1][j] && b[i][j] && b[i][j+1])continue;
if(!c[i][j])
{
k++;
dfs(i,j);
}
}
}
int a[k+5]={},ans=0,l=-1;
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<m ; j++){
if(c[i][j]!=0)
a[c[i][j]]++;
// cout<<c[i][j]<<" ";
}
// cout<<endl;
}
// cout<<k<<endl;
sort(a,a+k+5);
for(int i=0 ; i<k+5 ; i++){
// cout<<a[i]<<" ";
if(!a[i])continue;
ans+=l*a[i];
l*=-1;
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |