#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 (q.count(newmask) && q[newmask] < sum) return;
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 |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
29 ms |
5208 KB |
Output is correct |
3 |
Correct |
19 ms |
4952 KB |
Output is correct |
4 |
Correct |
25 ms |
4956 KB |
Output is correct |
5 |
Correct |
45 ms |
5212 KB |
Output is correct |
6 |
Correct |
57 ms |
5464 KB |
Output is correct |
7 |
Correct |
45 ms |
5468 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
16 ms |
4956 KB |
Output is correct |
10 |
Correct |
195 ms |
28520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
47 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
146 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
23 ms |
4528 KB |
Output is correct |
8 |
Correct |
26 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
29 ms |
5208 KB |
Output is correct |
3 |
Correct |
19 ms |
4952 KB |
Output is correct |
4 |
Correct |
25 ms |
4956 KB |
Output is correct |
5 |
Correct |
45 ms |
5212 KB |
Output is correct |
6 |
Correct |
57 ms |
5464 KB |
Output is correct |
7 |
Correct |
45 ms |
5468 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
16 ms |
4956 KB |
Output is correct |
10 |
Correct |
195 ms |
28520 KB |
Output is correct |
11 |
Correct |
3 ms |
4440 KB |
Output is correct |
12 |
Correct |
47 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
146 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
17 |
Correct |
23 ms |
4528 KB |
Output is correct |
18 |
Correct |
26 ms |
4444 KB |
Output is correct |
19 |
Correct |
7 ms |
4700 KB |
Output is correct |
20 |
Correct |
57 ms |
5732 KB |
Output is correct |
21 |
Correct |
128 ms |
6260 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
11 ms |
5024 KB |
Output is correct |
25 |
Correct |
388 ms |
7204 KB |
Output is correct |
26 |
Execution timed out |
1012 ms |
16464 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |