#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int_fast32_t n, m;
int_fast32_t dp[2][1 << 11];
char a[1000][10];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
int tp = 0;
for (int i : {0, 1}) {
for (int msk = 0; msk < (1 << m); ++msk) {
dp[i][msk] = 1e9;
}
}
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int nt = tp ^ 1;
for (int msk = 0; msk < (1 << m); ++msk) {
if (!((msk >> j) & 1)) {
int c = 0;
if ((a[i][j] == '#') && (!j || (a[i][j - 1] == '.') || ((msk >> (j - 1)) & 1))) {
c = 1;
}
dp[nt][msk] = min(dp[tp][msk], dp[tp][msk ^ (1 << j)]) + c;
} else {
if (a[i][j] == '.') {
dp[nt][msk] = 1e9;
} else {
dp[nt][msk] = min(dp[tp][msk], dp[tp][msk ^ (1 << j)] + 1);
}
}
}
tp ^= 1;
}
}
int_fast32_t ans = 1e9;
for (int msk = 0; msk < (1 << m); ++msk) {
ans = min(ans, dp[tp][msk]);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
18 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
9 ms |
364 KB |
Output is correct |
5 |
Correct |
19 ms |
364 KB |
Output is correct |
6 |
Correct |
19 ms |
364 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
21 ms |
364 KB |
Output is correct |
9 |
Correct |
18 ms |
364 KB |
Output is correct |
10 |
Correct |
20 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
18 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
9 ms |
364 KB |
Output is correct |
5 |
Correct |
19 ms |
364 KB |
Output is correct |
6 |
Correct |
19 ms |
364 KB |
Output is correct |
7 |
Correct |
18 ms |
364 KB |
Output is correct |
8 |
Correct |
21 ms |
364 KB |
Output is correct |
9 |
Correct |
18 ms |
364 KB |
Output is correct |
10 |
Correct |
20 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
364 KB |
Output is correct |
21 |
Correct |
10 ms |
364 KB |
Output is correct |
22 |
Correct |
17 ms |
364 KB |
Output is correct |
23 |
Correct |
18 ms |
492 KB |
Output is correct |
24 |
Correct |
18 ms |
364 KB |
Output is correct |
25 |
Correct |
20 ms |
364 KB |
Output is correct |
26 |
Correct |
20 ms |
492 KB |
Output is correct |
27 |
Correct |
27 ms |
384 KB |
Output is correct |
28 |
Correct |
31 ms |
436 KB |
Output is correct |