poj3363 Annoying painting tool

Annoying painting tool
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1736 Accepted: 1165

Description

Maybe you wonder what an annoying painting tool is? First of all, the painting tool we speak of supports only black and white. Therefore, a picture consists of a rectangular area of pixels, which are either black or white. Second, there is only one operation how to change the colour of pixels:

Select a rectangular area of r rows and c columns of pixels, which is completely inside the picture. As a result of the operation, each pixel inside the selected rectangle changes its colour (from black to white, or from white to black).

Initially, all pixels are white. To create a picture, the operation described above can be applied several times. Can you paint a certain picture which you have in mind?

Input

The input contains several test cases. Each test case starts with one line containing four integers nmr and c. (1 ≤ r ≤ n ≤ 100, 1 ≤ c ≤ m ≤ 100), The following n lines each describe one row of pixels of the painting you want to create. The ith line consists of m characters describing the desired pixel values of the ith row in the finished painting ('0' indicates white, '1' indicates black).

The last test case is followed by a line containing four zeros.

Output

For each test case, print the minimum number of operations needed to create the painting, or -1 if it is impossible.

Sample Input

3 3 1 1
010
101
010
4 3 2 1
011
110
011
110
3 4 2 2
0110
0111
0000
0 0 0 0

Sample Output

4
6

-1

贪心思想,从上到下从左到右,如果这一块不是0,就一定要进行一个翻转操作,因为,这是个矩形,一定是在左上角翻转是最好的

因为这样,翻转有可能把接下来的方块一并翻转了,所以,只需要最后比较一个,是否达到要求即可!

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define N 105
#define SCANF scanf_s
int map[N][N];
void print(int n,int m){
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cout<<map[i][j];
		}
		cout << endl;
	}
}
int main()
{
	int n, sum, m, r, c;
	while (SCANF("%d%d%d%d", &n, &m, &r, &c) != EOF)
	{
		if (n == 0 && m == 0 && r == 0 && c == 0)
			break;
		getchar();
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				map[i][j] = getchar() == '0' ? 0 : 1;
			}
			getchar();
		}
		sum = 0;
		for (int i = 0; i < n - r +1;i++)
		for (int j = 0; j < m - c +1; j++){
			if (map[i][j]){
				for (int k = i; k < i + r;k++)
				for (int s = j; s < j + c; s++){
					map[k][s] = map[k][s] ? 0 : 1;
				}
				sum++;
			}
		}
		//print(n, m);
		bool flag = true;
		for (int i = 0; i < n && flag;i++)
		for (int j = 0; j < m&& flag;j++)
		if (map[i][j]){
			sum = -1;
			flag = false;
		}
			
		cout << sum << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值