#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define crep(i,x,n) for(int i=x;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3HaBFkZ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
const int inf=1e9;
void chmin(int &a,int b){
a=min(a,b);
}
int main(){
_3HaBFkZ;
int n,m;
cin>>n>>m;
vec(vi) a(n,vi(n));
rep(i,n)rep(j,m){
char ch; cin>>ch;
a[i][j]=ch=='#';
}
vec(vi) dp(n+1,vi(1<<m,inf));
dp[0][0]=0;
rep(i,n){
rep(msk,1<<m)rep(j,m){
if(!(msk&(1<<j)) and a[i][j]){
chmin(dp[i][msk|(1<<j)],dp[i][msk]+1);
}
}
drep(msk,1<<m)rep(j,m){
if(msk&(1<<j)){
chmin(dp[i][msk^(1<<j)],dp[i][msk]);
}
}
vi nedp(1<<m,inf);
rep(msk,1<<m){
if(dp[i][msk]==inf) continue;
bool pok=1;
rep(j,m){
if(msk&(1<<j) and a[i][j]==0) pok=0;
}
if(!pok) continue;
int cost=0;
for(int j=0;j<m;){
if(!a[i][j] or msk&(1<<j)){
j=j+1;
continue;
}
cost++;
while(a[i][j]==1 and j<m and !(msk&(1<<j))){
j=j+1;
}
}
chmin(nedp[msk],dp[i][msk]+cost);
}
dp[i+1]=nedp;
}
int ans=*min_element(all(dp[n]));
cout<<ans<<"\n";
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
71 ms |
7304 KB |
Output is correct |
3 |
Correct |
16 ms |
4556 KB |
Output is correct |
4 |
Correct |
37 ms |
5964 KB |
Output is correct |
5 |
Correct |
77 ms |
8012 KB |
Output is correct |
6 |
Correct |
81 ms |
8140 KB |
Output is correct |
7 |
Correct |
85 ms |
7756 KB |
Output is correct |
8 |
Correct |
71 ms |
7380 KB |
Output is correct |
9 |
Correct |
84 ms |
7372 KB |
Output is correct |
10 |
Correct |
160 ms |
8268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
71 ms |
7304 KB |
Output is correct |
3 |
Correct |
16 ms |
4556 KB |
Output is correct |
4 |
Correct |
37 ms |
5964 KB |
Output is correct |
5 |
Correct |
77 ms |
8012 KB |
Output is correct |
6 |
Correct |
81 ms |
8140 KB |
Output is correct |
7 |
Correct |
85 ms |
7756 KB |
Output is correct |
8 |
Correct |
71 ms |
7380 KB |
Output is correct |
9 |
Correct |
84 ms |
7372 KB |
Output is correct |
10 |
Correct |
160 ms |
8268 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
4044 KB |
Output is correct |
20 |
Correct |
21 ms |
5196 KB |
Output is correct |
21 |
Correct |
47 ms |
6220 KB |
Output is correct |
22 |
Correct |
78 ms |
7928 KB |
Output is correct |
23 |
Correct |
77 ms |
7852 KB |
Output is correct |
24 |
Correct |
74 ms |
7708 KB |
Output is correct |
25 |
Correct |
93 ms |
8012 KB |
Output is correct |
26 |
Correct |
128 ms |
7588 KB |
Output is correct |
27 |
Correct |
145 ms |
7608 KB |
Output is correct |
28 |
Correct |
161 ms |
7884 KB |
Output is correct |