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;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen ("file.inp","r",stdin);
// freopen ("file.out","w",stdout);
int n, m;
cin >> n >> m;
vector<string> s(n);
vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(1 << m, 1e9)));
for (string &x : s)
cin >> x;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int mask = 0; mask < (1 << m); mask++)
if (mask & (1 << j))
{
if (s[i][j] == '.')
dp[i][j][mask] = 1e9;
else if (!j)
{
if (!i)
dp[i][j][mask] = (mask != 1 ? 1e9 : 1);
else
dp[i][j][mask] = min(dp[i - 1][m - 1][mask], dp[i - 1][m - 1][mask ^ (1 << j)] + 1);
}
else
dp[i][j][mask] = min(dp[i][j - 1][mask], dp[i][j - 1][mask ^ (1 << j)] + 1);
}
else
{
if (!j)
{
if (!i)
dp[i][j][mask] = (mask != 0 ? 1e9 : (s[i][j] == '#'));
else
dp[i][j][mask] = min(dp[i - 1][m - 1][mask], dp[i - 1][m - 1][mask ^ (1 << j)]) + (s[i][j] == '#');
}
else
{
dp[i][j][mask] = min(dp[i][j - 1][mask], dp[i][j - 1][mask ^ (1 << j)]);
if (s[i][j] == '#' && (!j || mask & (1 << (j - 1)) || s[i][j - 1] == '.'))
dp[i][j][mask]++;
}
}
int ans = 1e9;
for (int i = 0; i < (1 << m); i++)
ans = min(ans, dp[n - 1][m - 1][i]);
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... |