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;
int n, m;
bool arr[1001][11];
map <int, int> q;
inline bool check (int &x, int y) {
return (x >> y) & 1;
}
void recurse (int pos, int mask, int newmask, int cur, int sum) {
if (cur == m) {
if (q.count(newmask)) q[newmask] = min(q[newmask], sum);
else q[newmask] = sum;
return;
}
if (arr[pos][cur] == 0) {
recurse(pos, mask, newmask, cur + 1, sum);
return;
}
if (check(mask, cur)) {
recurse(pos, mask, newmask + (1 << cur), cur + 1, sum);
} else {
recurse(pos, mask, newmask + (1 << cur), cur + 1, sum + 1);
}
bool c = (cur != 0 && arr[pos][cur - 1] == 1 && check(newmask, cur - 1) == 0);
recurse(pos, mask, newmask, cur + 1, sum + (c ^ 1));
}
string get2 (int x);
int dp[1001][1 << 10];
int ans (int pos, int mask) {
if (pos == 0) return 0;
int &ret = dp[pos][mask];
if (ret != -1) return ret;
recurse(pos, mask, 0, 0, 0);
int mn = 1e9;
auto t = q; q.clear();
for (auto i : t) mn = min(mn, i.second + ans(pos - 1, i.first));
return ret = mn;
}
string get2 (int x) {
string s;
for (int i = 0; i < m; i++) s += (char)(check(x, i) + '0');
return s;
}
int main () {
memset(dp, -1, sizeof(dp));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
char x; cin >> x; arr[i][j] = x == '#';
}
}
cout << ans(n, 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... |