#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
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]);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
99 ms |
348 KB |
Output is correct |
3 |
Correct |
12 ms |
344 KB |
Output is correct |
4 |
Correct |
33 ms |
348 KB |
Output is correct |
5 |
Correct |
107 ms |
348 KB |
Output is correct |
6 |
Correct |
108 ms |
344 KB |
Output is correct |
7 |
Correct |
114 ms |
348 KB |
Output is correct |
8 |
Correct |
99 ms |
348 KB |
Output is correct |
9 |
Correct |
100 ms |
348 KB |
Output is correct |
10 |
Correct |
234 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
99 ms |
348 KB |
Output is correct |
3 |
Correct |
12 ms |
344 KB |
Output is correct |
4 |
Correct |
33 ms |
348 KB |
Output is correct |
5 |
Correct |
107 ms |
348 KB |
Output is correct |
6 |
Correct |
108 ms |
344 KB |
Output is correct |
7 |
Correct |
114 ms |
348 KB |
Output is correct |
8 |
Correct |
99 ms |
348 KB |
Output is correct |
9 |
Correct |
100 ms |
348 KB |
Output is correct |
10 |
Correct |
234 ms |
476 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
13 ms |
468 KB |
Output is correct |
21 |
Correct |
37 ms |
348 KB |
Output is correct |
22 |
Correct |
104 ms |
456 KB |
Output is correct |
23 |
Correct |
102 ms |
456 KB |
Output is correct |
24 |
Correct |
103 ms |
344 KB |
Output is correct |
25 |
Correct |
113 ms |
596 KB |
Output is correct |
26 |
Correct |
152 ms |
344 KB |
Output is correct |
27 |
Correct |
224 ms |
452 KB |
Output is correct |
28 |
Correct |
318 ms |
596 KB |
Output is correct |