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>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define pb push_back
#define fi first
#define se second
const ll N = 1005;
const ll M = 10;
const ll INF = 1e9;
ll n,m;
char grid[N][M+2];
ll memo[N][M+2][(1 << M)+5][2];
ll dp(ll x, ll y, ll mask, ll stat) {
if(x == n+1) return 0;
ll temp = memo[x][y][mask][stat];
if(temp != -1) return temp;
temp = INF;
if(y == m+1) temp = dp(x+1,1,mask,0);
else {
if(mask >> (y-1) & 1) {
if(grid[x][y] == '.') {
temp = min(temp,dp(x,y+1,mask-(1 << (y-1)),0));
}
else {
temp = min({temp,dp(x,y+1,mask,0), dp(x,y+1,mask-(1 << (y-1)),1) + (stat^1)});
}
}
else {
if(grid[x][y] == '.') {
temp = min(temp,dp(x,y+1,mask,0));
}
else {
temp = min({temp,dp(x,y+1,mask + (1 << (y-1)),0)+1, dp(x,y+1,mask,1)+(stat^1)});
}
}
}
memo[x][y][mask][stat] = temp;
return memo[x][y][mask][stat];
}
int main() {
memset(memo,-1,sizeof(memo));
cin >> n >> m;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin >> grid[i][j];
}
}
cout << dp(1,1,0,0) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |