#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define sz(x) (int)x.size()
#define M(x) (1ll << (x))
#define getBit(x, i) ((x) >> (i) & 1)
#define rep(i, b, e) for (ll i = (b); i <= (e); i++)
#define rrep(i, b, e) for (ll i = (b); i >= (e); i--)
typedef long long ll;
typedef pair<ll, ll> ii;
template <class T>
bool minimize(T &a, const T &b) {
if(a > b) {a = b; return 1;}
return 0;
}
template <class T>
bool maximize(T &a, const T &b) {
if(a < b) {a = b; return 1;}
return 0;
}
const int N = 1e3 + 7, M = 10;
int n, m, a[N][M + 2], f[N][M][1 << M];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, 1, n) {
rep(j, 0, m - 1) {
char c; cin >> c;
if (c == '#') a[i][j] = 1;
}
}
memset(f, 0x3f, sizeof f);
int ans = 2e9;
rep(j, 0, (1 << m) - 1) f[0][m - 1][j] = 0;
rep(i, 1, n) rep(j, 0, m - 1) rep(k, 0, (1 << m) - 1) {
if (a[i][j]) {
// verticle
if (getBit(k, j)) {
if (j == 0) {
minimize(f[i][j][k], f[i - 1][m - 1][k ^ M(j)] + 1);
if (a[i - 1][j]) minimize(f[i][j][k], f[i - 1][m - 1][k]);
}
else {
minimize(f[i][j][k], f[i][j - 1][k ^ M(j)] + 1);
if (a[i - 1][j]) minimize(f[i][j][k], f[i][j - 1][k]);
}
} else { // horizontal
if (j == 0) {
f[i][j][k] = min(f[i - 1][m - 1][k ^ M(j)], f[i - 1][m - 1][k]) + 1;
}
else {
if (!a[i][j - 1] || getBit(k, j - 1)) {
f[i][j][k] = min(f[i][j - 1][k], f[i][j - 1][k ^ M(j)]) + 1;
}
if (a[i][j - 1] && !getBit(k, j - 1))
f[i][j][k] = min(f[i][j - 1][k ^ M(j)], f[i][j - 1][k]);
}
}
}
else {
if (j == 0) f[i][j][k] = min(f[i - 1][m - 1][k], f[i - 1][m - 1][k ^ M(j)]);
else f[i][j][k] = min(f[i][j - 1][k], f[i][j - 1][k ^ M(j)]);
}
if (i == n && j == m - 1) minimize(ans, f[i][j][k]);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
40592 KB |
Output is correct |
2 |
Correct |
38 ms |
40724 KB |
Output is correct |
3 |
Correct |
21 ms |
40660 KB |
Output is correct |
4 |
Correct |
26 ms |
40660 KB |
Output is correct |
5 |
Correct |
37 ms |
40660 KB |
Output is correct |
6 |
Correct |
45 ms |
40660 KB |
Output is correct |
7 |
Correct |
36 ms |
40612 KB |
Output is correct |
8 |
Correct |
33 ms |
40652 KB |
Output is correct |
9 |
Correct |
39 ms |
40720 KB |
Output is correct |
10 |
Correct |
37 ms |
40796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
40660 KB |
Output is correct |
2 |
Correct |
19 ms |
40660 KB |
Output is correct |
3 |
Correct |
17 ms |
40660 KB |
Output is correct |
4 |
Correct |
18 ms |
40652 KB |
Output is correct |
5 |
Correct |
17 ms |
40612 KB |
Output is correct |
6 |
Correct |
18 ms |
40672 KB |
Output is correct |
7 |
Correct |
18 ms |
40628 KB |
Output is correct |
8 |
Correct |
18 ms |
40660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
40592 KB |
Output is correct |
2 |
Correct |
38 ms |
40724 KB |
Output is correct |
3 |
Correct |
21 ms |
40660 KB |
Output is correct |
4 |
Correct |
26 ms |
40660 KB |
Output is correct |
5 |
Correct |
37 ms |
40660 KB |
Output is correct |
6 |
Correct |
45 ms |
40660 KB |
Output is correct |
7 |
Correct |
36 ms |
40612 KB |
Output is correct |
8 |
Correct |
33 ms |
40652 KB |
Output is correct |
9 |
Correct |
39 ms |
40720 KB |
Output is correct |
10 |
Correct |
37 ms |
40796 KB |
Output is correct |
11 |
Correct |
18 ms |
40660 KB |
Output is correct |
12 |
Correct |
19 ms |
40660 KB |
Output is correct |
13 |
Correct |
17 ms |
40660 KB |
Output is correct |
14 |
Correct |
18 ms |
40652 KB |
Output is correct |
15 |
Correct |
17 ms |
40612 KB |
Output is correct |
16 |
Correct |
18 ms |
40672 KB |
Output is correct |
17 |
Correct |
18 ms |
40628 KB |
Output is correct |
18 |
Correct |
18 ms |
40660 KB |
Output is correct |
19 |
Correct |
18 ms |
40684 KB |
Output is correct |
20 |
Correct |
22 ms |
40692 KB |
Output is correct |
21 |
Correct |
27 ms |
40740 KB |
Output is correct |
22 |
Correct |
38 ms |
40716 KB |
Output is correct |
23 |
Correct |
33 ms |
40628 KB |
Output is correct |
24 |
Correct |
35 ms |
40720 KB |
Output is correct |
25 |
Correct |
42 ms |
40736 KB |
Output is correct |
26 |
Correct |
41 ms |
40724 KB |
Output is correct |
27 |
Correct |
43 ms |
40684 KB |
Output is correct |
28 |
Correct |
44 ms |
40656 KB |
Output is correct |