#include <bits/stdc++.h>
using namespace std;
const int oo = 2e9;
const int N = 1010;
const int M = 10;
const int PW = 10;
const int HPW = 233;
const int md = int(1e9) + 7;
int f[N][(1 << M)], n, m;
string s;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i <= n; i++)
for (int j = 0; j < (1 << m); j++)
f[i][j] = oo;
f[0][0] = 0;
for (int i = 1; i <= n; i++){
cin >> s;
int cur = 0;
for (int j = 0; j < m; j++)
if (s[j] == '#')
cur += (1 << j);
for (int old = 0; old < (1 << m); old++)
for (int msk = old; ; msk = ((msk - 1) & old)){
f[i - 1][msk] = min(f[i - 1][msk], f[i - 1][old]);
if (msk == 0) break;
}
for (int nw = 0; nw < (1 << m); nw++){
bool ok = 1;
for (int j = 0; j < m; j++)
if (s[j] == '.' && (nw & (1 << j))){
ok = 0;
break;
}
if (!ok) continue;
int base = 0;
for (int j = 0; j < m; j++)
if (nw & (1 << j))
base++;
else {
if (cur & (1 << j)){
if (!(j > 0 && (cur & (1 << (j - 1))) && (!(nw & (1 << (j - 1))))))
base++;
}
}
for (int pre = nw; ; pre = ((pre - 1) & nw)){
int nf = f[i - 1][pre];
if (nf == oo) {
if (pre == 0)
break;
continue;
}
nf += base - __builtin_popcount(pre & nw);
f[i][nw] = min(f[i][nw], nf);
if (pre == 0) break;
}
}
}
int ans = oo;
for (int msk = 0; msk < (1 << m); msk++)
ans = min(ans, f[n][msk]);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
81 ms |
4076 KB |
Output is correct |
3 |
Correct |
14 ms |
4076 KB |
Output is correct |
4 |
Correct |
33 ms |
4204 KB |
Output is correct |
5 |
Correct |
86 ms |
4460 KB |
Output is correct |
6 |
Correct |
90 ms |
4332 KB |
Output is correct |
7 |
Correct |
91 ms |
4256 KB |
Output is correct |
8 |
Correct |
87 ms |
4076 KB |
Output is correct |
9 |
Correct |
95 ms |
4112 KB |
Output is correct |
10 |
Correct |
165 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
81 ms |
4076 KB |
Output is correct |
3 |
Correct |
14 ms |
4076 KB |
Output is correct |
4 |
Correct |
33 ms |
4204 KB |
Output is correct |
5 |
Correct |
86 ms |
4460 KB |
Output is correct |
6 |
Correct |
90 ms |
4332 KB |
Output is correct |
7 |
Correct |
91 ms |
4256 KB |
Output is correct |
8 |
Correct |
87 ms |
4076 KB |
Output is correct |
9 |
Correct |
95 ms |
4112 KB |
Output is correct |
10 |
Correct |
165 ms |
4332 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
4 ms |
4204 KB |
Output is correct |
20 |
Correct |
16 ms |
4352 KB |
Output is correct |
21 |
Correct |
37 ms |
4332 KB |
Output is correct |
22 |
Correct |
85 ms |
4332 KB |
Output is correct |
23 |
Correct |
81 ms |
4332 KB |
Output is correct |
24 |
Correct |
85 ms |
4332 KB |
Output is correct |
25 |
Correct |
91 ms |
4204 KB |
Output is correct |
26 |
Correct |
129 ms |
4076 KB |
Output is correct |
27 |
Correct |
226 ms |
4204 KB |
Output is correct |
28 |
Correct |
369 ms |
4328 KB |
Output is correct |