This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<ll, ll>
#define ld long double
#define nl '\n'
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define sz take.size()
#define arr3 array<int, 3>
#define calc(x, y) (sqr(X[x]-X[y])+sqr(Y[x]-Y[y]))
using namespace std;
const ll MX=1005, MOD=1000000007, BLOCK=327, INF=2e9+7;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
int N, M, DP[MX][12][1030], ans=INF;
string A[MX];
int main(){
fastio;
cin >> N >> M;
for(int i=1; i<=N; i++){
cin >> A[i];
}
for(int j=0; j<M; j++){
for(int mask=0; mask<(1<<M); mask++)
DP[0][j][mask]=INF;
}
DP[0][M-1][0]=0;
A[0]=string(M, '.');
for(int i=1; i<=N; i++){
for(int j=0; j<M; j++){
for(int mask=0; mask<(1<<M); mask++){
int x=i, y=j-1;
if(y<0)
x=i-1, y=M-1;
if(mask&(1<<j)){
if(A[i][j]=='.'){
DP[i][j][mask]=INF;
}else{
DP[i][j][mask]=min(DP[x][y][mask^(1<<j)]+1, DP[x][y][mask]);
}
}else{
DP[i][j][mask]=min(DP[x][y][mask], DP[x][y][mask^(1<<j)]);
if(A[i][j]=='#'){
if(j==0 || A[i][j-1]=='.' || (mask&(1<<(j-1))))
DP[i][j][mask]++;
}
}
}
}
}
for(int mask=0; mask<(1<<M); mask++)
ans=min(ans, DP[N][M-1][mask]);
cout << ans << nl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |