#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n, m;
cin >> n >> m;
vector<string>cal(n);
for (string &r : cal) {
cin >> r;
}
vector<vvi>dp(n + 1, vvi(m + 1, vi(1 << m, oo)));
for (int p = 0; p < (1 << m); ++p) {
dp[0][0][p] = 0;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int p = 0; p < (1 << m); ++p) {
if (cal[i][j] == '.') {
dp[i][j + 1][p] = min(dp[i][j + 1][p], dp[i][j][p]);
dp[i][j + 1][p ^ (1 << j)] = min(dp[i][j + 1][p ^ (1 << j)], dp[i][j][p]);
} else {
int h = (j && cal[i][j - 1] == '#' && (p & (1 << (j - 1)))) ? 0 : 1;
int q = p | (1 << j);
dp[i][j + 1][q] = min(dp[i][j + 1][q], dp[i][j][p] + h);
int v = (i && cal[i - 1][j] == '#' && !(p & (1 << j))) ? 0 : 1;
q ^= (1 << j);
dp[i][j + 1][q] = min(dp[i][j + 1][q], dp[i][j][p] + v);
}
}
}
for (int p = 0; p < (1 << m); ++p) {
dp[i + 1][0][p] = dp[i][m][p];
}
}
int res = oo;
for (int p = 0; p < (1 << m); ++p) {
res = min(res, dp[n][0][p]);
}
cout << res << ln;
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
42 ms |
41428 KB |
Output is correct |
3 |
Correct |
9 ms |
9044 KB |
Output is correct |
4 |
Correct |
21 ms |
20148 KB |
Output is correct |
5 |
Correct |
48 ms |
44164 KB |
Output is correct |
6 |
Correct |
51 ms |
44372 KB |
Output is correct |
7 |
Correct |
54 ms |
43300 KB |
Output is correct |
8 |
Correct |
41 ms |
41420 KB |
Output is correct |
9 |
Correct |
43 ms |
41684 KB |
Output is correct |
10 |
Correct |
50 ms |
44996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
42 ms |
41428 KB |
Output is correct |
3 |
Correct |
9 ms |
9044 KB |
Output is correct |
4 |
Correct |
21 ms |
20148 KB |
Output is correct |
5 |
Correct |
48 ms |
44164 KB |
Output is correct |
6 |
Correct |
51 ms |
44372 KB |
Output is correct |
7 |
Correct |
54 ms |
43300 KB |
Output is correct |
8 |
Correct |
41 ms |
41420 KB |
Output is correct |
9 |
Correct |
43 ms |
41684 KB |
Output is correct |
10 |
Correct |
50 ms |
44996 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
840 KB |
Output is correct |
19 |
Correct |
2 ms |
1236 KB |
Output is correct |
20 |
Correct |
10 ms |
9684 KB |
Output is correct |
21 |
Correct |
25 ms |
20792 KB |
Output is correct |
22 |
Correct |
41 ms |
43512 KB |
Output is correct |
23 |
Correct |
42 ms |
43312 KB |
Output is correct |
24 |
Correct |
46 ms |
43024 KB |
Output is correct |
25 |
Correct |
48 ms |
43988 KB |
Output is correct |
26 |
Correct |
52 ms |
42240 KB |
Output is correct |
27 |
Correct |
59 ms |
42316 KB |
Output is correct |
28 |
Correct |
65 ms |
43832 KB |
Output is correct |