poj3363 Annoying painting tool
来源:互联网 发布:网络诈骗1000元 编辑:程序博客网 时间:2024/05/24 06:34
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 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 10101010104 3 2 10111100111103 4 2 20110011100000 0 0 0
Sample Output
46-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_sint 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;}
- poj3363 Annoying painting tool
- 14 POJ3363 Annoying painting tool
- 2908. Annoying painting tool
- hdu 1801 Annoying painting tool
- Annoying painting tool+POJ+水题
- POJ-3363 Annoying painting tool
- PKU3363 Annoying painting tool - 超级构造法
- POJ 3363 Annoying painting tool 再看看
- (贪心5.2.10)POJ 3363 Annoying painting tool
- 杂码系列之 Annoying painting tool hdoj 1801
- poj 1753 Flip Game 枚举+递推 poj 3363 Annoying painting tool
- POJ3363
- Tool
- tool
- tool
- tool
- Tool
- tool
- openfire+jwchat
- linux下查看进程/线程
- 修改postgresql用户密码
- Android几个常用功能代码(校园助手开发笔记)
- Hibernate对象的三种状态
- poj3363 Annoying painting tool
- 安全密码
- Java程序员们最常犯的10个错误
- TableView的一些需要掌握的东西
- Editions and Components of SQL Server 2008 R2
- C#遍历类的属性,然后给其赋值
- easyUI的datagrid每行数据添加操作按钮的方法
- startActivity()详解
- App崩溃的原因(iOS)