POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 19356 | Accepted: 7412 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i,
j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is
initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions,
you may give any one of them.
Sample Input
- -+--
- ----
- ----
- -+--
Sample Output
- 6
- 1 1
- 1 3
- 1 4
- 4 1
- 4 3
- 4 4
Source
看着ACM练习建议去做的,,上面说的是枚举。。
可是我枚举了半天。。没搞出来
网上搜了下别人的题解,都是说有高效贪心算法。然后琢磨半天搞懂了
比方:
-+--
----
----
----
要把这个改成全是减号就必须将+号所在行和所在列的符号所有都改变一次(+号所在位置改变一次就可以)
所以有例如以下改变次数:
4744
2422
2422
2422
当中7是+号位置所需改变的次数,4是+号所在行和所在列(不包含+号)所需改变次数
因此得出高效解法,在每次输入碰到'+'的时候, 计算所在行和列的须要改变的次数
当输入结束后,遍历数组,全部为奇数的位置则是操作的位置,而奇数位置的个数之和则是终于的操作次数.
但这样的算法仅仅对n为偶数时适用
PS:该题不会有"Impossible"的情况.
AC代码:
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int a[5][5];
- int main()
- {
- char c;
- memset(a, 0, sizeof(a));
- for(int i=1; i<=4; i++)
- for(int j=1; j<=4; j++)
- {
- c = getchar();
- while(c == '\n') c = getchar();
- if(c == '+')
- {
- for(int k=1; k<=4; k++)
- {
- a[i][k]++;
- a[k][j]++;
- }
- a[i][j]--;
- }
- }
- int sum = 0;
- for(int i=1; i<=4; i++)
- for(int j=1; j<=4; j++)
- sum += a[i][j]%2;
- printf("%d\n", sum);
- for(int i=1; i<=4; i++)
- for(int j=1; j<=4; j++)
- if(a[i][j]%2)
- printf("%d %d\n", i, j);
- return 0;
- }
POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)的更多相关文章
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965:The Pilots Brothers' refrigerator
id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
随机推荐
- c# 利用反射动态给实体类对象赋值
转:http://blog.sina.com.cn/s/blog_659a572b0100xp5s.html 例子如下 using System; using System.Collections.G ...
- C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)
一.效果与思路 效果: 解决方案1 用chart的mousemove时间,实时跟踪鼠标最近的X轴的位置,然后把cursorX设置到那个位置上,让用户知道我是选的那一个X的值,同时用tooltip显示该 ...
- android让你的TabHost滑动起来
在Android应用中,一般TabActivity和若干个Tab选项卡(TabWidget).如果选项卡的数量超过了5个,就不适合放到一个屏幕中,这样可以让这些选项卡滑动起来. 滑动的选项卡的实现有好 ...
- 陈发树云南白药股权败诉真相 取胜仅差三步 z
22亿元现金,三年只拿到750多万元的利息.福建富豪陈发树的云南生意可谓失望之极.在漫长的官司中,曾经有绝处逢生之机的陈发树,连告状的主体都没有找准,岂能同强大的国企扳手腕?陈发树律师团距取胜只有三步 ...
- Matlab编程实例(1) 移动平均
MATLAB数字信号处理作业,把自己写的程序发上来..欢迎交流~ QQ 五幺九七九零六四 首先是任意点移动平均: 主程序:mov_average_main.m (运行) 函数:mov_averag ...
- 关于DISPLAY变量显示问题
DISPLAY用来设置将图形显示到何处 在本机上直接登录之后,startx,可以发现display变量自动设置为 [oracle@kel ~]$ echo $DISPLAY :0.0 不论使用何种用户 ...
- Spring 中context.start作用
我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...
- js笔记--json
1.什么事json: json是指javascript对象表示法(javascript Object Notation), json是轻量级的文本数据交换格式: json独立于语言: json具有自我 ...
- 多校6 1001 HDU5793 A Boring Question (推公式 等比数列求和)
题解:http://bestcoder.hdu.edu.cn/blog/ 多校6 HDU5793 A Boring Question // #pragma comment(linker, " ...
- 关于OpenCV做图像处理内存释放的一些问题
转载:http://blog.sina.com.cn/s/blog_67a7426a0101czyr.html 工程运行,发现内存持续增长,到一定的时候就发生了内存泄漏. 内存泄露的定义 内存泄露是说 ...