#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define ld long double
signed main()
{
//ifstream cin("286.txt");
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//cout.tie(0);
int n, m;
cin >> n >> m;
char A[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> A[i][j];
}
}
vector <int> dp((1 << m), 1e9), dp1((1 << m), 1e9);
dp[0] = 0;
for(int i = 0; i < n; i++)
{
fill(dp1.begin(), dp1.end(), 1e9);
int msk = 0;
for(int j = 0; j < m; j++)
{
msk += (A[i][j] == '#') * (1 << j);
}
for(int j = 0; j < (1 << m); j++)
{
dp1[msk & j] = min(dp1[msk & j], dp[j]);
}
for(int j = (1 << m) - 1; j >= 0; j--)
{
for(int t = 0; t < m; t++)
{
if((1 << t) & j)
{
continue;
}
dp1[j] = min(dp1[j], dp1[j + (1 << t)]);
}
}
for(int j = 0; j < (1 << m); j++)
{
for(int t = 0; t < m; t++)
{
if(!((1 << t) & j))
{
continue;
}
dp1[j] = min(dp1[j], dp1[j - (1 << t)] + 1);
}
}
/* for(int j = 0; j < (1 << m); j++)
{
cout << dp1[j] << " ";
}
cout << "\n";
*/ for(int j = 0; j < (1 << m); j++)
{
if((j & msk) != j)
{
// cout << "A " << j << "\n";
dp1[j] = 1e9;
continue;
}
int last = -2;
int cnt = 0;
for(int t = 0; t < m; t++)
{
if((1 << t) & j)
{
continue;
}
if((1 << t) & msk)
{
if(last < t - 1)
{
cnt++;
}
last = t;
}
}
dp1[j] += cnt;
}
for(int j = 0; j < (1 << m); j++)
{
dp[j] = dp1[j];
}
//cout << msk << " ";
//cout << dp[0] << "\n";
}
int msk = 0;
for(int i= 0; i < m; i++)
{
msk += (A[n - 1][i] == '#') * (1 << i);
}
int ans = 1e9;
for(int j = 0; j < (1 << m); j++)
{
if((msk & j) != j)
{
continue;
}
ans = min(ans, dp[j]);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
83 ms |
324 KB |
Output is correct |
3 |
Correct |
13 ms |
204 KB |
Output is correct |
4 |
Correct |
34 ms |
204 KB |
Output is correct |
5 |
Correct |
89 ms |
312 KB |
Output is correct |
6 |
Correct |
88 ms |
204 KB |
Output is correct |
7 |
Correct |
85 ms |
300 KB |
Output is correct |
8 |
Correct |
82 ms |
292 KB |
Output is correct |
9 |
Correct |
83 ms |
204 KB |
Output is correct |
10 |
Correct |
110 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
83 ms |
324 KB |
Output is correct |
3 |
Correct |
13 ms |
204 KB |
Output is correct |
4 |
Correct |
34 ms |
204 KB |
Output is correct |
5 |
Correct |
89 ms |
312 KB |
Output is correct |
6 |
Correct |
88 ms |
204 KB |
Output is correct |
7 |
Correct |
85 ms |
300 KB |
Output is correct |
8 |
Correct |
82 ms |
292 KB |
Output is correct |
9 |
Correct |
83 ms |
204 KB |
Output is correct |
10 |
Correct |
110 ms |
204 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
2 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
2 ms |
208 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
2 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
204 KB |
Output is correct |
20 |
Correct |
14 ms |
204 KB |
Output is correct |
21 |
Correct |
36 ms |
204 KB |
Output is correct |
22 |
Correct |
86 ms |
204 KB |
Output is correct |
23 |
Correct |
89 ms |
296 KB |
Output is correct |
24 |
Correct |
87 ms |
204 KB |
Output is correct |
25 |
Correct |
89 ms |
312 KB |
Output is correct |
26 |
Correct |
98 ms |
204 KB |
Output is correct |
27 |
Correct |
112 ms |
204 KB |
Output is correct |
28 |
Correct |
125 ms |
300 KB |
Output is correct |