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 X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define ld long double
signed main()
{
//ifstream cin("286.txt");
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//cout.tie(0);
int n, m;
cin >> n >> m;
char A[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> A[i][j];
}
}
vector <int> dp((1 << m), 1e9), dp1((1 << m), 1e9);
dp[0] = 0;
for(int i = 0; i < n; i++)
{
fill(dp1.begin(), dp1.end(), 1e9);
int msk = 0;
for(int j = 0; j < m; j++)
{
msk += (A[i][j] == '#') * (1 << j);
}
for(int j = 0; j < (1 << m); j++)
{
dp1[msk & j] = min(dp1[msk & j], dp[j]);
}
for(int j = (1 << m) - 1; j >= 0; j--)
{
for(int t = 0; t < m; t++)
{
if((1 << t) & j)
{
continue;
}
dp1[j] = min(dp1[j], dp1[j + t]);
}
}
for(int j = 0; j < (1 << m); j++)
{
for(int t = 0; t < m; t++)
{
if(!((1 << t) & j))
{
continue;
}
dp1[j] = min(dp1[j], dp1[j - (1 << t)] + 1);
}
}
/* for(int j = 0; j < (1 << m); j++)
{
cout << dp1[j] << " ";
}
cout << "\n";
*/ for(int j = 0; j < (1 << m); j++)
{
if((j & msk) != j)
{
// cout << "A " << j << "\n";
dp1[j] = 1e9;
continue;
}
int last = -2;
int cnt = 0;
for(int t = 0; t < m; t++)
{
if((1 << t) & j)
{
continue;
}
if((1 << t) & msk)
{
if(last < t - 1)
{
cnt++;
}
last = t;
}
}
dp1[j] += cnt;
}
for(int j = 0; j < (1 << m); j++)
{
dp[j] = dp1[j];
}
//cout << msk << " ";
//cout << dp[0] << "\n";
}
int msk = 0;
for(int i= 0; i < m; i++)
{
msk += (A[n - 1][i] == '#') * (1 << i);
}
int ans = 1e9;
for(int j = 0; j < (1 << m); j++)
{
if((msk & j) != j)
{
continue;
}
ans = min(ans, dp[j]);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |