This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n, m;
cin >> n >> m;
vector<string>cal(n);
for (string &r : cal) {
cin >> r;
}
// memory optimized version
vvi dp(2, vi(1 << m, 0));
fill(all(dp[1]), oo);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int p = 0; p < (1 << m); ++p) {
if (cal[i][j] == '.') {
dp[1][p] = min(dp[1][p], dp[0][p]);
dp[1][p ^ (1 << j)] = min(dp[1][p ^ (1 << j)], dp[0][p]);
} else {
int h = (j && cal[i][j - 1] == '#' && (p & (1 << (j - 1)))) ? 0 : 1;
int q = p | (1 << j);
dp[1][q] = min(dp[1][q], dp[0][p] + h);
int v = (i && cal[i - 1][j] == '#' && !(p & (1 << j))) ? 0 : 1;
q ^= (1 << j);
dp[1][q] = min(dp[1][q], dp[0][p] + v);
}
}
dp[0] = dp[1];
fill(all(dp[1]), oo);
}
}
int res = (*min_element(all(dp[0])));
cout << res << ln;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
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... |