# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 2000, inf = 1e9;
int t,n,m, dp[N][N],a[N][N], dphor[N],toadd[N];
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
char ch;
cin>>ch;
a[i][j] = (ch == '#');
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < (1<<m); j++) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
int all = 0;
for (int x = 0; x < m; x++) {
if (a[i][x]) all |= (1<<x);
}
for (int mask = 0; mask < (1<<m); mask++) {
// Using only horizontal chains
dphor[mask] = 0;
if ((mask|all) != all) continue;
for (int x = 0; x < m; x++) {
if (mask&(1<<x)) {
if (x == 0) {
dphor[mask]++; continue;
}
if (mask&(1<<(x - 1))) continue;
else dphor[mask]++;
}
}
}
for (int mask = 0; mask < (1<<m); mask++) toadd[mask] = inf;
for (int mask = 0; mask < (1<<m); mask++) {
// at least mask is used in the previous row
for (int x = mask; x >= 0; x = (x - 1)&mask) {
toadd[x] = min(toadd[x], dp[i - 1][mask]);
if (x == 0) break;
}
}
for (int mask = 0; mask < (1<<m); mask++) {
dp[i][mask] = inf;
// If a square is covered by a vertical chain ----- > mask[j] == 1
// else mask[j] = 0;
if ((mask | all) != all) continue;
for (int x = mask; x >= 0; x = (x - 1) & mask) {
dp[i][mask] = min(dp[i][mask], toadd[x] + __builtin_popcount(x ^ mask) + dphor[all ^ mask]);
if (x == 0) break;
}
}
}
int ans = inf;
for (int mask = 0; mask < (1<<m); mask++) ans = min(ans, dp[n][mask]);
cout<<ans<<"\n";
}
Compilation message
Main.cpp:10:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
10 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
80 ms |
11220 KB |
Output is correct |
3 |
Correct |
13 ms |
8612 KB |
Output is correct |
4 |
Correct |
31 ms |
9988 KB |
Output is correct |
5 |
Correct |
81 ms |
11988 KB |
Output is correct |
6 |
Correct |
85 ms |
12036 KB |
Output is correct |
7 |
Correct |
79 ms |
11748 KB |
Output is correct |
8 |
Correct |
74 ms |
11220 KB |
Output is correct |
9 |
Correct |
92 ms |
11220 KB |
Output is correct |
10 |
Correct |
199 ms |
12192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
80 ms |
11220 KB |
Output is correct |
3 |
Correct |
13 ms |
8612 KB |
Output is correct |
4 |
Correct |
31 ms |
9988 KB |
Output is correct |
5 |
Correct |
81 ms |
11988 KB |
Output is correct |
6 |
Correct |
85 ms |
12036 KB |
Output is correct |
7 |
Correct |
79 ms |
11748 KB |
Output is correct |
8 |
Correct |
74 ms |
11220 KB |
Output is correct |
9 |
Correct |
92 ms |
11220 KB |
Output is correct |
10 |
Correct |
199 ms |
12192 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
15 ms |
9228 KB |
Output is correct |
21 |
Correct |
35 ms |
10196 KB |
Output is correct |
22 |
Correct |
82 ms |
11732 KB |
Output is correct |
23 |
Correct |
79 ms |
11744 KB |
Output is correct |
24 |
Correct |
81 ms |
11672 KB |
Output is correct |
25 |
Correct |
85 ms |
11956 KB |
Output is correct |
26 |
Correct |
112 ms |
11476 KB |
Output is correct |
27 |
Correct |
178 ms |
11476 KB |
Output is correct |
28 |
Correct |
277 ms |
11860 KB |
Output is correct |