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>
#define ll long long
using namespace std;
const int INF = 2e9;
int main()
{
int n, m;
cin>>n>>m;
vector<string> grid(n);
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(m + 1, vector<int>((1<<m), INF)));
for(auto& i : grid) cin>>i;
for (int mask = 0; mask < (1<<m); mask++) dp[0][0][mask] = 0;
for(int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
for (int p = 0; p < (1<<m); p++)
{
if(grid[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);
int v = 1 - (i && grid[i - 1][j] == '#' && (p & (1<<j)));
dp[i][j + 1][q] = min(dp[i][j + 1][q], dp[i][j][p] + v);
q ^= (1<<j);
int h = 1 - (j && grid[i][j - 1] == '#' && !(p & (1<<(j - 1))));
dp[i][j + 1][q] = min(dp[i][j + 1][q], dp[i][j][p] + h);
}
}
}
for (int p = 0; p < (1<<m); p++) dp[i + 1][0][p] = dp[i][m][p];
}
cout << *min_element(dp[n][0].begin(), dp[n][0].end()) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |