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;
}

时间: 2024-08-01 10:45:24

poj3363 Annoying painting tool的相关文章

14 POJ3363 Annoying painting tool

给一个全0矩阵,每次你可以使得一个r*c的子矩阵01翻转,问是否可以通过这种操作使得矩阵变为题目给定的矩阵,最少需要多少次操作. 可以发现,对于第一行第一个点,它只能通过翻转包含(1,1)这个点在内的矩阵才能变,也就是r*c的左上端点在(1,1). 通过至多一次操作,我们可以保证这个点可以达到目标状态. 接着第二个点,我们首先不能影响第一个点,因此如果这个点与目标状态不同,我们只能以这个点为翻转矩阵的左上角,来翻转. 同样,通过至多一次操作第二个点也可以到达目标状态. 因此,我们从上到下,从左到

sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )

Annoying painting tool 题目描述 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.

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HTML5 canvas 做画板画图 可以做电子白板

HTML5 canvas 做画板画图 可以做电子白板 <html> <head> <meta charset="utf-8"> <title>HTML5 canvas 做画板画图 可以做电子白板</title> <style type="text/css"> <!-- #container { position: relative;} #imageView { border: 1px so

UNREAL ENGINE 4.12 正式发布!下载地址

UNREAL ENGINE 4.12 正式发布! 下载地址:https://www.unrealengine.com/ Alexander Paschall 在 June 1, 2016 |功能新闻社区 Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn 此版本内含虚幻引擎 4 的数百个更新,以及 GitHub 虚幻引擎开发者社区提交的 106 项改良!特此对虚幻引擎 4.12 版本的贡献者们表达诚挚谢意:

sdutoj 2373 Black and white painting( 规律统计计算 3Y )

Black and white painting 题目描述 You are visiting the Centre Pompidou which contains a lot of modern paintings. In particular you notice one painting which consists solely of black and white squares, arranged in rows and columns like in a chess board (n

在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite

最近在学习spring,用到的IDE 有eclipse,也有用到 IDEA. 目前对spring还不是很了解,跟着网上的视频来,先学会了spring,然后再选IDE. 题归正转,下面说说怎么在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite. 打开eclipse,然后在菜单栏中点击Help,选中Eclipse MarketPlace: 然后搜索STS(也就是spring tool suite的缩写),回车: 点击Install即可 由于是在线安装