2908. Annoying painting tool

水题

题目大意是输入n和m,代表有n行m列像素点,初始所有的像素均为白色,即为0,然后给定一个r*c的举证,在这个范围内发像素均改变它的颜色,黑变白,白变黑,问有至少多少次操作使最终变成给定的像素矩阵。

所以用反向思维,求最终用多少次操作使得像素矩阵最后为全白。

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 n, m, r 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

#include<iostream>
using namespace std;
char map[101][101];
int main()
{
	int n,m,r,c,ans;
	while(cin>>n>>m>>r>>c&&n){
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				cin>>map[i][j];
		ans=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='1')
				{
					if(i+r>n||j+c>m)
						continue;
					ans++;
					for(int k=i;k<i+r;k++)
						for(int l=j;l<j+c;l++)
							map[k][l]=((map[k][l]-'0')^1)+'0';
				}
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(map[i][j]!='0')
				{
					ans=-1;
					break;
				}
			}
				if(ans==-1)
					break;
			
		}
		cout<<ans<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++和segment tree解决下述问题Doing Exercises 描述 As we all know, the lines of students doing exercises between classes are always unsatisfactory to teachers. Today, a teacher wants to require something new. Firstly, he lets some students of N classes correspondingly form n lines. Then, he randomly selects a class to add some of its remaining students to its line, or selects a class to let some students leave its line, or lets the monitors from some adjacent classes report the total number of students in all these classes. This is very annoying for the monitors. Can you write a program to help them complete the reporting task? 输入 The first line is an integer T (T<50), the number of test cases. For each test case, its first line is an integer N (1<=N<=50000), representing the number of classes, and its second line include N integers (a1, a2, a3, ... , an), and ai (1<=ai<=100) means the number of students in the line of class i at the beginning. Then, each next line will be an order. There are 4 kinds of orders: (1) "Add x i" means adding x students to the line of class i; (2) "Sub x i" means that x students leave the line of class i; (3) "Query i j" means that the monitors from class i to class j report the teacher the total number (sum) of students in their classes at that moment (i<j); (4) "End" means ending the exercises, which will only appear at the end of each test case. The number of orders will not exceed 40000. The number of students in any line will never below 0. 输出 For each test case, you must print "Case i:" in the first line. Then for each Query, print the result in one line.
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值