#include <bits/stdc++.h>
using namespace std;
#ifndef _LOCAL
//#pragma GCC optimize("O3,Ofast")
#else
#pragma GCC optimize("O0")
#endif
template<typename t> inline void umin(t &a, const t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, const t b) {a = max(a, b);}
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef int8_t byte;
ll time() {return chrono::system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
#define ft first
#define sd second
#define len(f) int((f).size())
#define bnd(f) (f).begin(), (f).end()
#define _ <<' '<<
const int inf = 1e9 + 5;
const ll inf64 = 4e18 + 5;
const int md = 998244353;
namespace MD {
void add(int &a, const int b) {if((a += b) >= md) a -= md;}
void sub(int &a, const int b) {if((a -= b) < 0) a += md;}
int prod(const int a, const int b) {return ll(a) * b % md;}
};
void solve() {
int n, m;
cin >> n >> m;
bool mp[n][m];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
char c; cin >> c;
mp[i][j] = c == '#';
}
}
int k = 1 << m;
int dp[n + 1][m][k];
memset(dp, 127, sizeof dp);
dp[0][0][0] = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
for(int ms = 0; ms < k; ++ms) {
int c = dp[i][j][ms];
if(c > +inf) continue;
int nxj = j + 1, nxi = i;
if(nxj == m) nxj = 0, ++nxi;
if(mp[i][j]) {
umin(dp[nxi][nxj][ms | 1 << j], c + !(1 << j & ms));
umin(dp[nxi][nxj][ms & ~(1 << j)], c + (!j || !mp[i][j - 1] || (ms & 1 << j - 1)));
} else {
umin(dp[nxi][nxj][ms & ~(1 << j)], c);
}
}
}
}
int ans = +inf;
for(int i = 0; i < k; ++i)
umin(ans, dp[n][0][i]);
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef _LOCAL
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
#else
system("color a");
freopen("in.txt", "r", stdin);
int t; cin >> t;
while(t--)
#endif
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:53:97: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | umin(dp[nxi][nxj][ms & ~(1 << j)], c + (!j || !mp[i][j - 1] || (ms & 1 << j - 1)));
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
32 ms |
37240 KB |
Output is correct |
3 |
Correct |
8 ms |
7788 KB |
Output is correct |
4 |
Correct |
17 ms |
17900 KB |
Output is correct |
5 |
Correct |
37 ms |
39788 KB |
Output is correct |
6 |
Correct |
37 ms |
39916 KB |
Output is correct |
7 |
Correct |
36 ms |
38892 KB |
Output is correct |
8 |
Correct |
34 ms |
37356 KB |
Output is correct |
9 |
Correct |
34 ms |
37484 KB |
Output is correct |
10 |
Correct |
40 ms |
40428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
32 ms |
37240 KB |
Output is correct |
3 |
Correct |
8 ms |
7788 KB |
Output is correct |
4 |
Correct |
17 ms |
17900 KB |
Output is correct |
5 |
Correct |
37 ms |
39788 KB |
Output is correct |
6 |
Correct |
37 ms |
39916 KB |
Output is correct |
7 |
Correct |
36 ms |
38892 KB |
Output is correct |
8 |
Correct |
34 ms |
37356 KB |
Output is correct |
9 |
Correct |
34 ms |
37484 KB |
Output is correct |
10 |
Correct |
40 ms |
40428 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
16 |
Correct |
1 ms |
748 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
748 KB |
Output is correct |
19 |
Correct |
1 ms |
1004 KB |
Output is correct |
20 |
Correct |
9 ms |
8300 KB |
Output is correct |
21 |
Correct |
19 ms |
18412 KB |
Output is correct |
22 |
Correct |
32 ms |
39148 KB |
Output is correct |
23 |
Correct |
34 ms |
38892 KB |
Output is correct |
24 |
Correct |
34 ms |
38764 KB |
Output is correct |
25 |
Correct |
38 ms |
39660 KB |
Output is correct |
26 |
Correct |
46 ms |
37996 KB |
Output is correct |
27 |
Correct |
58 ms |
38124 KB |
Output is correct |
28 |
Correct |
76 ms |
39404 KB |
Output is correct |