You've got an n × m pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture.
A picture is a barcode if the following conditions are fulfilled:
- All pixels in each column are of the same color.
- The width of each monochrome vertical line is at least x and at most y pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than x or greater than y.
The first line contains four space-separated integers n, m, x and y (1 ≤ n, m, x, y ≤ 1000; x ≤ y).
Then follow n lines, describing the original image. Each of these lines contains exactly m characters. Character "." represents a white pixel and "#" represents a black pixel. The picture description doesn't have any other characters besides "." and "#".
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.
6 5 1 2 ##.#. .###. ###.. #...# .##.# ###..
11
2 5 1 1 ##### .....
5
In the first test sample the picture after changing some colors can looks as follows:
.##.. .##.. .##.. .##.. .##.. .##..
In the second test sample the picture after changing some colors can looks as follows:
.#.#. .#.#.
这道题过的有一点点小奇怪,知道了状态转移方程之后,我写出的程序总是与答案差一,于是减一之,过之。。。。
题意:要求序列的每一列都变成相同的字符,且连续的#串的列数要在x与y之间,求 最少变几次。
状态转移方程:dp[i+j][0]=min(dp[i+j][0],dp[i][1]+(a[i+j]-a[i]));
dp[i+j][1]=min(dp[i+j][1],dp[i][0]+j*n-(a[i+j]-a[i]));
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
int n,m,x,y,i,j,k;
int dp[1005][2],a[1005];
char b[1005][1005];
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&x,&y))
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=0;i<n;i++)
{
scanf("%s",b[i]);
for(j=0;j<m;j++)
if(b[i][j]=='#')
a[j+1]++;
}
for(i=1;i<m;i++)
a[i+1]+=a[i];
for(i=0;i<=m;i++)
dp[i][0]=dp[i][1]=m*n;
dp[0][1]=dp[0][0]=1;
for(i=0;i<m;i++)
{
for(j=x;j<=y&&i+j<=m;j++)
{
dp[i+j][0]=min(dp[i+j][0],dp[i][1]+(a[i+j]-a[i]));
dp[i+j][1]=min(dp[i+j][1],dp[i][0]+j*n-(a[i+j]-a[i]));
//cout<<dp[i+j][0]<<"***"<<dp[i+j][1]<<" ";
}
//cout<<endl;
}
cout<<min(dp[m][0],dp[m][1])-1<<endl;
}
return 0;
}