//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define REB(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e3 + 7;
const int Mod = 1e9 + 7;///lon
const int INF = 1e9 + 7;
const ll BASE = 137;
const int szBL = 450;
inline void minimize (int &A, int B) {
if (A > B) A = B;
}
int n, m;
int a[N][N];
int dp[N][15][(1 << 10) + 7];
void solution () {
cin >> n >> m;
rep (i, 1, n) {
string s;
cin >> s;
rep (j, 1, m) {
a[i][j] = s[j - 1] == '#' ? 1 : 0;
}
}
memset(dp, 0x3f, sizeof dp);
if (a[1][1] == 1) {
dp[1][1][1] = dp[1][1][0] = 1;
}
else {
dp[1][1][0] = 0;
}
auto make = [&] (int msk, int i, int typ) -> int {
return msk ^ ((bit(msk, i) ^ typ) << i);
};
rep (i, 1, n) {
rep (j, 1, m) {
rep (msk, 0, (1 << m) - 1) {
if (j < m) {
if (a[i][j + 1] == 0) {
int nmsk = make(msk, j, 0);
minimize (dp[i][j + 1][nmsk], dp[i][j][msk]);
}
else {
rep (k, 0, 1) {
int W = 1 - (bit(msk, j) && k == 1) - (a[i][j] && bit(msk, j - 1) == 0 && k == 0);
int nmsk = make(msk, j, k);
minimize (dp[i][j + 1][nmsk], dp[i][j][msk] + W);
}
}
}
else {
if (a[i + 1][1] == 0) {
int nmsk = make(msk, 0, 0);
minimize (dp[i + 1][1][nmsk], dp[i][j][msk]);
}
else {
rep (k, 0, 1) {
int W = 1 - (bit(msk, 0) && k == 1);
int nmsk = make(msk, 0, k);
minimize (dp[i + 1][1][nmsk], dp[i][j][msk] + W);
}
}
}
}
}
}
int res = INF;
rep (msk, 0, (1 << m) - 1) {
res = min(res, dp[n][m][msk]);
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +32
rep (i, 0, n) {
rep (j, 0, i) {
if (j == 0 || j == i) {
C[j][i] = 1;
continue;
}
C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;
}
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |