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. 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?

输入

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 nlines 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.

输出

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

示例输入

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

示例输出

4
6
-1

代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <string.h> using namespace std; int map[110][110];
int n, m, r, c;
bool judge(int dd, int ff)
{
if((dd+r-1)<=n && (ff+c-1)<=m )
return true;
else
return false;
} void OP(int dd, int ff)
{
int i, j;
for(i=dd; i<=(dd+r-1); i++)
{
for(j=ff; j<=(ff+c-1); j++)
{
if(map[i][j]==1)
map[i][j]=0;
else
map[i][j]=1;
}
}
} int main()
{
int flag;
int i, j, k, e;
char s[110];
int cnt;
while(scanf("%d %d %d %d", &n, &m, &r, &c)!=EOF)
{
if(n==0&&m==0&&r==0&&c==0 )
break;
flag=1;
cnt=0;
for(i=1; i<=n; i++)
{
scanf("%s", s);
int len=strlen(s);
for(j=0; j<len; j++)
map[i][j+1]=s[j]-48;
}
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(map[i][j]==1)
{
if(judge(i, j)==true)
{
OP(i, j); cnt++;
}
else
{
flag=0; break;
}
}
}
if(flag==0) break;
}
if(flag==0)
printf("-1\n");
else
printf("%d\n", cnt );
}
return 0;
}

sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )的更多相关文章

  1. BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定

    题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...

  2. [Swust OJ 763]--校门外的树 Plus(暴力枚举)

    题目链接:http://acm.swust.edu.cn/problem/0763/ Time limit(ms): 1000 Memory limit(kb): 65535 西南某科技大学的校门外有 ...

  3. SDUT OJ 1221 亲和数 (找出某个数n所有的因子数,只需要暴力:2->sqrt(n) 即可 )

    亲和数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 如果a的因子和等于b,b的因子和等于a,且a≠b,则称a,b为亲和数对. ...

  4. SDUT OJ 2607

    /*http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607*/ 题目大意:给出一个字符串,求出里 ...

  5. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  6. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  7. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  8. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  9. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

随机推荐

  1. 2016-2017 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2016)

    题目链接  Codefores_Gym_101164 Solved  6/11 Penalty Problem A Problem B Problem C Problem D Problem E Pr ...

  2. ML | spectral clustering

    What's xxx In multivariate statistics and the clustering of data, spectral clustering techniques mak ...

  3. 同源策略Same-origin policy

     同源策略Same-origin policy 同源策略Same-origin policy是Web应用的一种安全基础策略.它规定同一源中,页面包含的脚本可以访问该源下的其他页面的数据.只有当网址中的 ...

  4. 海量端口扫描工具masscan

    海量端口扫描工具masscan   masscan号称是互联网上最快的端口扫描工具,可以6分钟扫描整个互联网,每秒可以发送一百万个数据包.为了提高处理速度,masscan定制了TCP/IP栈,从而不影 ...

  5. Socks5代理Socks5 Proxy

    Socks5代理Socks5 Proxy   Socks5代理是基于Socks协议的一种代理模式.其中,5表示该协议的版本号.它可以让局域网的计算机通过socks5代理服务器,访问外网的内容.由于它工 ...

  6. List遍历时删除遇到的问题

    这周在开发中遇到了一个以前没遇到的小Bug,在这里记录下来. List集合,我们平时都经常使用.但是,我在遍历List集合时,调用了List集合的remove方法来删除集合中的元素,简单的代码结构是这 ...

  7. 布斯(Steve Jobs)在斯坦福大学的演讲稿,中英文对照版

    2005年6月14日,苹果CEO史蒂夫·乔布斯(Steve Jobs)在他的母校斯坦福大学的毕业典礼发表了著名的演讲,关于这段演讲,你会看到N多人的推荐(比如同样喜欢在大学演讲的李开复先生).此前曾经 ...

  8. PyTorch学习笔记之Tensors

    PyTorch Tensors are just like numpy arrays, but they can run on GPU.No built-in notion of computatio ...

  9. 算法之美--2.3.1 Z字形编排问题

    2016-12-08   00:23:11 写在前面的话:万事贵在坚持,万事开头难,有很多的东西要学,要知道主次,讲究效率,大的方向对就行!坚持........ 一.图像压缩编码中的Z字排序 JPEG ...

  10. react request.js 函数封装

    1.request.js  函数封装 import { Toast } from 'antd-mobile'; import axios from 'axios'; import store from ...