#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int N = 1000, INF = 1e9;
const ll mod = 1e9 + 7;
int n, m, dp[11][(1 << 10)];
bool a[N][10];
int main () {
ios::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++){
char x;
cin >> x, a[i][j] = (x != '#');
}
}
for(int i = 0;i <= m;i++)
for(int mask = 0;mask < (1 << m);mask++)
dp[i][mask] = INF;
dp[m][(1 << m) - 1] = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
for(int mask = 0;mask < (1 << m);mask++){
if(a[i][j] && !((mask >> j) & 1))
continue;
int last = j - 1;
if(last == -1)
last = m;
if(!a[i][j]){
if((mask >> j) & 1)
dp[j][mask ^ (1 << j)] = min(dp[j][mask ^ (1 << j)], dp[last][mask]);
else
dp[j][mask] = min(dp[j][mask], dp[last][mask]);
}
dp[j][mask | (1 << j)] = min(dp[j][mask | (1 << j)], dp[last][mask] + !a[i][j]);
int nx = j;
while(nx < m && !a[i][nx] && ((mask >> nx) & 1)){
dp[nx][mask] = min(dp[nx][mask], dp[last][mask] + 1);
nx++;
}
}
}
for(int mask = 0;mask < (1 << m);mask++)
dp[m][mask] = dp[m - 1][mask];
for(int i = 0;i < m;i++)
for(int mask = 0;mask < (1 << m);mask++)
dp[i][mask] = INF;
}
cout << dp[m][(1 << m) - 1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
37 ms |
364 KB |
Output is correct |
3 |
Correct |
10 ms |
364 KB |
Output is correct |
4 |
Correct |
20 ms |
364 KB |
Output is correct |
5 |
Correct |
43 ms |
364 KB |
Output is correct |
6 |
Correct |
45 ms |
364 KB |
Output is correct |
7 |
Correct |
45 ms |
364 KB |
Output is correct |
8 |
Correct |
36 ms |
364 KB |
Output is correct |
9 |
Correct |
43 ms |
364 KB |
Output is correct |
10 |
Correct |
59 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
37 ms |
364 KB |
Output is correct |
3 |
Correct |
10 ms |
364 KB |
Output is correct |
4 |
Correct |
20 ms |
364 KB |
Output is correct |
5 |
Correct |
43 ms |
364 KB |
Output is correct |
6 |
Correct |
45 ms |
364 KB |
Output is correct |
7 |
Correct |
45 ms |
364 KB |
Output is correct |
8 |
Correct |
36 ms |
364 KB |
Output is correct |
9 |
Correct |
43 ms |
364 KB |
Output is correct |
10 |
Correct |
59 ms |
364 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 |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
12 ms |
364 KB |
Output is correct |
21 |
Correct |
37 ms |
364 KB |
Output is correct |
22 |
Correct |
31 ms |
364 KB |
Output is correct |
23 |
Correct |
34 ms |
364 KB |
Output is correct |
24 |
Correct |
40 ms |
492 KB |
Output is correct |
25 |
Correct |
53 ms |
364 KB |
Output is correct |
26 |
Correct |
62 ms |
384 KB |
Output is correct |
27 |
Correct |
75 ms |
364 KB |
Output is correct |
28 |
Correct |
83 ms |
364 KB |
Output is correct |