POJ-3363 Annoying painting tool
来源:互联网 发布:电脑c盘windows 编辑:程序博客网 时间:2024/04/29 03:47
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 integersn, m, r and c. (1 ≤ r ≤ n ≤ 100, 1 ≤ c ≤ m ≤ 100), The followingn lines each describe one row of pixels of the painting you want to create. Theith line consists of m characters describing the desired pixel values of theith 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
————————————————————尴尬的分割线————————————————————
前言:许久之前的代码了,但是不知道为什么没有发题解。。。补上
思路:贪心。给定的刷子会把某区域内的颜色全部反转,因为刷两下和不刷是一模一样的,所以从左上角开始,一旦遇到了1,就刷一下。一个一个枚举,到最后如果图上还存在1,那就是不可能消除的。
代码如下:
/****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> using namespace std;/****************************************/bool mat[105][105];//bool mat[105][105];int main(){int n, m, r, c;while(~scanf("%d%d%d%d", &n, &m, &r, &c), n||m||r||c) {char t;for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {scanf(" %c", &t);mat[i][j] = t - '0';}}int op = 0;bool ok = true;for(int i = 0; i+r <= n; i++) {for(int j = 0; j+c <= m; j++) {if(mat[i][j] == 1) {op++;for(int k = i; k < i+r; k++) {for(int kk = j; kk < j+c; kk++) {mat[k][kk] = !mat[k][kk];}}}}}for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {if(mat[i][j] == 1) {ok = false;break;}}}printf("%d\n", ok ? op : -1);}return 0;}
- POJ-3363 Annoying painting tool
- POJ 3363 Annoying painting tool 再看看
- Annoying painting tool+POJ+水题
- (贪心5.2.10)POJ 3363 Annoying painting tool
- 2908. Annoying painting tool
- poj3363 Annoying painting tool
- poj 1753 Flip Game 枚举+递推 poj 3363 Annoying painting tool
- hdu 1801 Annoying painting tool
- 14 POJ3363 Annoying painting tool
- PKU3363 Annoying painting tool - 超级构造法
- 杂码系列之 Annoying painting tool hdoj 1801
- poj 1054Annoying frog(枚举)
- POJ 1691 Painting A Board
- poj 1691 Painting A Board
- poj 1691 Painting A Board
- POJ 1691 Painting A Board
- POJ 1691 Painting A Board
- 【poj 1691】Painting A Board
- Node.js 博客实例(四)实现用户页面和文章页面
- 一段代码和多幅图让你明白http
- poj2942--F - Knights of the Round Table(圆桌骑士,经典连通分量)
- 大话重构连载11:小步快跑是这样玩的
- JPA性能方法------EntityManager的find()与getReference()的区别
- POJ-3363 Annoying painting tool
- 拷贝构造函数
- DAISY描述子简介
- Palindrome Partitioning I and II leetcode
- 今天的天气不错
- 条件变量和临界区的一个例子
- 看来不是没有
- Non-Photorealistic Rendering (Domain transform for edge-aware image and video processing)
- 机房重构之模板方法模式