#include<bits/stdc++.h>
#define INF 1e9+7
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pipii pair<int,pii>
#define plpll pair<ll,pll>
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ins insert
#define ln '\n'
#define all(v) v.begin(),v.end()
using namespace std;
void solve(){
int n,m;
cin>>n>>m;
vector<string>inp(n);
for(string &to:inp){
cin>>to;
}
vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(m+1,vector<int>(1<<m,INF)));
for(int p=0;p<(1<<m);p++){
dp[0][0][p]=0;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int p=0;p<(1<<m);p++){
if(inp[i][j]=='.'){
dp[i][j+1][p]=min(dp[i][j+1][p],dp[i][j][p]);
dp[i][j+1][p^(1<<j)]=min(dp[i][j+1][p^(1<<j)],dp[i][j][p]);
}
else{
int q=p|(1<<j);
if(j&&inp[i][j-1]=='#'&&p&(1<<(j-1))){
dp[i][j+1][q]=min(dp[i][j+1][q],dp[i][j][p]);
}
else{
dp[i][j+1][q]=min(dp[i][j+1][q],dp[i][j][p]+1);
}
q^=(1<<j);
if(i&&inp[i-1][j]=='#'&&!(p&(1<<j))){
dp[i][j+1][q]=min(dp[i][j+1][q],dp[i][j][p]);
}
else{
dp[i][j+1][q]=min(dp[i][j+1][q],dp[i][j][p]+1);
}
}
}
}
for(int p=0;p<(1<<m);p++){
dp[i+1][0][p]=dp[i][m][p];
}
}
int res=INF;
for(int p=0;p<(1<<m);p++){
res=min(res,dp[n][0][p]);
}
cout<<res<<ln;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
/*
10 10
0 1 10 100
1 1 5 3
1 7 9 8
0 1 10 9
2 1 2 3
2 9 4
0 1 10 100
1 1 10 2
0 1 10 1000
2 1 5 2
0 1 6
1
0
ll
10
1 2
2 4
4 8
4 9
2 5
1 3
3 6
6 10
3 7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
42 ms |
41428 KB |
Output is correct |
3 |
Correct |
10 ms |
8916 KB |
Output is correct |
4 |
Correct |
21 ms |
20164 KB |
Output is correct |
5 |
Correct |
46 ms |
44168 KB |
Output is correct |
6 |
Correct |
46 ms |
44372 KB |
Output is correct |
7 |
Correct |
45 ms |
43220 KB |
Output is correct |
8 |
Correct |
41 ms |
41420 KB |
Output is correct |
9 |
Correct |
43 ms |
41596 KB |
Output is correct |
10 |
Correct |
51 ms |
44884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
836 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
832 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
832 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
42 ms |
41428 KB |
Output is correct |
3 |
Correct |
10 ms |
8916 KB |
Output is correct |
4 |
Correct |
21 ms |
20164 KB |
Output is correct |
5 |
Correct |
46 ms |
44168 KB |
Output is correct |
6 |
Correct |
46 ms |
44372 KB |
Output is correct |
7 |
Correct |
45 ms |
43220 KB |
Output is correct |
8 |
Correct |
41 ms |
41420 KB |
Output is correct |
9 |
Correct |
43 ms |
41596 KB |
Output is correct |
10 |
Correct |
51 ms |
44884 KB |
Output is correct |
11 |
Correct |
1 ms |
836 KB |
Output is correct |
12 |
Correct |
2 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
832 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
1236 KB |
Output is correct |
20 |
Correct |
12 ms |
9556 KB |
Output is correct |
21 |
Correct |
25 ms |
20804 KB |
Output is correct |
22 |
Correct |
41 ms |
43476 KB |
Output is correct |
23 |
Correct |
44 ms |
43220 KB |
Output is correct |
24 |
Correct |
45 ms |
42964 KB |
Output is correct |
25 |
Correct |
58 ms |
44092 KB |
Output is correct |
26 |
Correct |
52 ms |
42260 KB |
Output is correct |
27 |
Correct |
55 ms |
42324 KB |
Output is correct |
28 |
Correct |
57 ms |
43824 KB |
Output is correct |