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;
const int N = 1000 + 5, M = 10, K = 1 << M;
int n, m, dp[N][M][K];
char arr[N][M];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 0; j < m; j++){
cin >> arr[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < m; j++){
for(int msk = 0; msk < (1 << m); msk++){
if(j == 0){
if(msk & (1 << j)){
dp[i][j][msk] = min(dp[i - 1][m - 1][msk] + (arr[i][j] == '#' && arr[i - 1][j] != '#'), dp[i - 1][m - 1][msk ^ (1 << j)] + (arr[i][j] == '#'));
}
else{
dp[i][j][msk] = min(dp[i - 1][m - 1][msk], dp[i - 1][m - 1][msk ^ (1 << j)]) + (arr[i][j] == '#');
}
}
else{
if(msk & (1 << j)){
dp[i][j][msk] = min(dp[i][j - 1][msk] + (arr[i][j] == '#' && arr[i - 1][j] != '#'), dp[i][j - 1][msk ^ (1 << j)] + (arr[i][j] == '#'));
}
else{
if(!(msk & (1 << (j - 1)))){
dp[i][j][msk] = min(dp[i][j - 1][msk], dp[i][j - 1][msk ^ (1 << j)]) + (arr[i][j] == '#' && arr[i][j - 1] != '#');
}
else{
dp[i][j][msk] = min(dp[i][j - 1][msk], dp[i][j - 1][msk ^ (1 << j)]) + (arr[i][j] == '#');
}
}
}
}
}
}
cout << *min_element(dp[n][m - 1], dp[n][m - 1] + (1 << m));
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |