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 pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m,dp[1<<15],val[1<<15],a[15];
char s[1005][15];
int solve(char* str,int mask){
for(int i=0;i<m;i++) a[i]=(str[i]=='#')^(mask>>i&1);
int ret=0;
for(int i=0;i<m;i++) if(a[i]==1&&(i==0||a[i-1]==0)) ret++;
return ret;
}
int main(){
n=readint(); m=readint();
for(int i=1;i<=n;i++) scanf("%s",s[i]);
for(int mask=1;mask<(1<<m);mask++) val[mask]=1e9;
for(int i=1;i<=n;i++){
for(int mask=0;mask<(1<<m);mask++) dp[mask]=1e9;
for(int mask=0;mask<(1<<m);mask++){
bool ok=true;
for(int j=0;j<m;j++) if((mask>>j&1)&&s[i][j]=='.') ok=false;
if(!ok) continue;
int ft=solve(s[i],mask);
dp[mask]=val[0]+__builtin_popcount(mask)+ft;
for(int submask=mask;submask;submask=(submask-1)&mask)
dp[mask]=min(dp[mask],val[submask]+__builtin_popcount(mask^submask)+ft);
}
for(int mask=0;mask<(1<<m);mask++) val[mask]=1e9;
for(int mask=0;mask<(1<<m);mask++){
for(int submask=mask;submask;submask=(submask-1)&mask)
val[submask]=min(val[submask],dp[mask]);
val[0]=min(val[0],dp[mask]);
}
}
int ans=1e9;
for(int mask=0;mask<(1<<m);mask++) ans=min(ans,dp[mask]);
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:38:29: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | for(int i=1;i<=n;i++) scanf("%s",s[i]);
| ~~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |