#include <bits/stdc++.h>
#define fore(i, a, b) for (ll i = (a), _b = (b); i < _b; ++i)
#define fort(i, a, b) for (ll i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for (ll i = (a), _b = (b); i >= _b; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define getbit(x, i) ((x) & (1 << (i)))
#define off(x, i) ((x) & (~(1 << (i))))
#define on(x, i) ((x) | (1 << (i)))
using namespace std;
template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const int maxN = 1000, maxM = 10, inf = 1e9;
int n, m, dp[maxN][maxM][1 << maxM][2];
bool vis[maxN][maxM][1 << maxM][2];
char s[maxN][maxM];
int DP(const int r, const int c, const int vertical, const bool horizontal) {
if (r >= n)
return 0;
if (!vis[r][c][vertical][horizontal]) {
vis[r][c][vertical][horizontal] = true;
dp[r][c][vertical][horizontal] = inf;
if (c + 1 < m) {
if (s[r][c] == '.')
mini(dp[r][c][vertical][horizontal], DP(r, c + 1, off(vertical, c), false));
else {
if (getbit(vertical, c))
mini(dp[r][c][vertical][horizontal], DP(r, c + 1, vertical, false));
else
mini(dp[r][c][vertical][horizontal], DP(r, c + 1, on(vertical, c), false) + 1);
mini(dp[r][c][vertical][horizontal], DP(r, c + 1, off(vertical, c), true) + !horizontal);
}
} else {
if (s[r][c] == '.')
mini(dp[r][c][vertical][horizontal], DP(r + 1, 0, off(vertical, c), false));
else {
if (getbit(vertical, c))
mini(dp[r][c][vertical][horizontal], DP(r + 1, 0, vertical, false));
else
mini(dp[r][c][vertical][horizontal], DP(r + 1, 0, on(vertical, c), false) + 1);
mini(dp[r][c][vertical][horizontal], DP(r + 1, 0, off(vertical, c), false) + !horizontal);
}
}
}
return dp[r][c][vertical][horizontal];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
fore(i, 0, n)
cin >> s[i];
cout << DP(0, 0, 0, false) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1900 KB |
Output is correct |
2 |
Correct |
47 ms |
72684 KB |
Output is correct |
3 |
Correct |
41 ms |
65004 KB |
Output is correct |
4 |
Correct |
47 ms |
73836 KB |
Output is correct |
5 |
Correct |
57 ms |
84972 KB |
Output is correct |
6 |
Correct |
58 ms |
89196 KB |
Output is correct |
7 |
Correct |
62 ms |
93804 KB |
Output is correct |
8 |
Correct |
45 ms |
74604 KB |
Output is correct |
9 |
Correct |
52 ms |
84076 KB |
Output is correct |
10 |
Correct |
82 ms |
87660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1260 KB |
Output is correct |
2 |
Correct |
2 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1260 KB |
Output is correct |
4 |
Correct |
2 ms |
1388 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1900 KB |
Output is correct |
2 |
Correct |
47 ms |
72684 KB |
Output is correct |
3 |
Correct |
41 ms |
65004 KB |
Output is correct |
4 |
Correct |
47 ms |
73836 KB |
Output is correct |
5 |
Correct |
57 ms |
84972 KB |
Output is correct |
6 |
Correct |
58 ms |
89196 KB |
Output is correct |
7 |
Correct |
62 ms |
93804 KB |
Output is correct |
8 |
Correct |
45 ms |
74604 KB |
Output is correct |
9 |
Correct |
52 ms |
84076 KB |
Output is correct |
10 |
Correct |
82 ms |
87660 KB |
Output is correct |
11 |
Correct |
1 ms |
1260 KB |
Output is correct |
12 |
Correct |
2 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1260 KB |
Output is correct |
14 |
Correct |
2 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1004 KB |
Output is correct |
16 |
Correct |
1 ms |
1132 KB |
Output is correct |
17 |
Correct |
1 ms |
1132 KB |
Output is correct |
18 |
Correct |
1 ms |
1260 KB |
Output is correct |
19 |
Correct |
22 ms |
31852 KB |
Output is correct |
20 |
Correct |
50 ms |
74348 KB |
Output is correct |
21 |
Correct |
62 ms |
87916 KB |
Output is correct |
22 |
Correct |
37 ms |
60780 KB |
Output is correct |
23 |
Correct |
43 ms |
70124 KB |
Output is correct |
24 |
Correct |
52 ms |
83052 KB |
Output is correct |
25 |
Correct |
70 ms |
96364 KB |
Output is correct |
26 |
Correct |
115 ms |
95212 KB |
Output is correct |
27 |
Correct |
167 ms |
95596 KB |
Output is correct |
28 |
Correct |
237 ms |
98796 KB |
Output is correct |