#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
return l + rd() % (r - l + 1);
}
const int N = 1e3 + 5;
const int mod = 1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int inf = 1e9; // INT_MAX;
const long long llinf = 1e18; // LLONG_MAX;
template<class X, class Y>
bool minimize(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y>
bool maximize(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
template<class X, class Y>
bool add(X &a, Y b) {
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return true;
}
int n, m;
char c[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> c[i][j];
vector<int> pf(1 << m, inf), f(1 << m);
pf[0] = 0;
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
for (int mask = 0; mask < bit(m); ++mask) {
if (getbit(mask, j)) {
if (c[i][j] == '.') f[mask] = inf;
else f[mask] = min(pf[mask], pf[mask ^ bit(j)] + 1);
}
else {
f[mask] = min(pf[mask], pf[mask ^ bit(j)]);
if (c[i][j] == '#' && (j == 0 || getbit(mask, j - 1) || c[i][j - 1] == '.')) f[mask]++;
}
}
pf = f;
}
cout << *min_element(all(pf));
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
1160 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
10 ms |
1432 KB |
Output is correct |
5 |
Correct |
17 ms |
1372 KB |
Output is correct |
6 |
Correct |
18 ms |
1372 KB |
Output is correct |
7 |
Correct |
17 ms |
1372 KB |
Output is correct |
8 |
Correct |
16 ms |
1368 KB |
Output is correct |
9 |
Correct |
18 ms |
1396 KB |
Output is correct |
10 |
Correct |
17 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
1160 KB |
Output is correct |
3 |
Correct |
4 ms |
1372 KB |
Output is correct |
4 |
Correct |
10 ms |
1432 KB |
Output is correct |
5 |
Correct |
17 ms |
1372 KB |
Output is correct |
6 |
Correct |
18 ms |
1372 KB |
Output is correct |
7 |
Correct |
17 ms |
1372 KB |
Output is correct |
8 |
Correct |
16 ms |
1368 KB |
Output is correct |
9 |
Correct |
18 ms |
1396 KB |
Output is correct |
10 |
Correct |
17 ms |
1372 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
1240 KB |
Output is correct |
20 |
Correct |
5 ms |
1244 KB |
Output is correct |
21 |
Correct |
9 ms |
1372 KB |
Output is correct |
22 |
Correct |
15 ms |
1368 KB |
Output is correct |
23 |
Correct |
16 ms |
1368 KB |
Output is correct |
24 |
Correct |
17 ms |
1372 KB |
Output is correct |
25 |
Correct |
18 ms |
1368 KB |
Output is correct |
26 |
Correct |
23 ms |
1372 KB |
Output is correct |
27 |
Correct |
22 ms |
1412 KB |
Output is correct |
28 |
Correct |
21 ms |
1624 KB |
Output is correct |