Barcode
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
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.
Input
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 “#”.
Output
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.
Sample Input
Input
6 5 1 2
.#.
.###.
..
…
.##.#
..
Output
11
Input
2 5 1 1
#
…..
Output
5
Hint
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:
.#.#.
.#.#.
先统计每列的黑白图形的个数,然后就进行DP了~~感觉不是很难吧~~
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int n,m,x,y,w[MAXN],b[MAXN],dp[2][MAXN];
char st[MAXN];
int main()
{
scanf("%d %d %d %d",&n,&m,&x,&y);
fill(w + 1,w + 1 + m,0);
fill(b + 1,b + 1 + m,0);
memset(dp,1,sizeof(dp));
for(int i = 1 ; i <= n ; i++){
scanf("%s",st + 1);
for(int j = 1 ; j <= m ; j++)
if(st[j] == '#') w[j]++;
else b[j]++;
}
for(int i = 1 ; i <= m ; i++) w[i] += w[i-1],b[i] += b[i - 1];
dp[0][0] = dp[1][0] = 0;
for(int i = 0 ; i < m ; i++)
for(int j = x ; j <= y ; j++){ // 列的宽度
dp[0][i + j] = min(dp[0][i + j],dp[1][i] + w[i + j] - w[i]);
dp[1][i + j] = min(dp[1][i + j],dp[0][i] + b[i + j] - b[i]);
}
printf("%d\n",min(dp[0][m],dp[1][m]));
return 0;
}