#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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
24 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
340 KB |
Output is correct |
4 |
Correct |
12 ms |
340 KB |
Output is correct |
5 |
Correct |
27 ms |
344 KB |
Output is correct |
6 |
Correct |
27 ms |
340 KB |
Output is correct |
7 |
Correct |
26 ms |
340 KB |
Output is correct |
8 |
Correct |
23 ms |
468 KB |
Output is correct |
9 |
Correct |
27 ms |
344 KB |
Output is correct |
10 |
Correct |
30 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
24 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
340 KB |
Output is correct |
4 |
Correct |
12 ms |
340 KB |
Output is correct |
5 |
Correct |
27 ms |
344 KB |
Output is correct |
6 |
Correct |
27 ms |
340 KB |
Output is correct |
7 |
Correct |
26 ms |
340 KB |
Output is correct |
8 |
Correct |
23 ms |
468 KB |
Output is correct |
9 |
Correct |
27 ms |
344 KB |
Output is correct |
10 |
Correct |
30 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
7 ms |
340 KB |
Output is correct |
21 |
Correct |
16 ms |
364 KB |
Output is correct |
22 |
Correct |
22 ms |
360 KB |
Output is correct |
23 |
Correct |
24 ms |
372 KB |
Output is correct |
24 |
Correct |
24 ms |
340 KB |
Output is correct |
25 |
Correct |
30 ms |
340 KB |
Output is correct |
26 |
Correct |
34 ms |
356 KB |
Output is correct |
27 |
Correct |
35 ms |
340 KB |
Output is correct |
28 |
Correct |
39 ms |
340 KB |
Output is correct |