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 ...
随机推荐
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- POJ 2136 Vertical Histogram
题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...
- 类装载器ClassLoader
类装载器工作机制 类装载器就是寻找类的节码文件并构造出类在JVM内部表示对象的组件.在Java中,类装载器把一个类装入JVM中,要经过以下步骤: [1.]装载:查找和导入Class文件: [2.]链接 ...
- 使用buildbot实现持续集成(转载)
转载自:http://www.oschina.net/p/buildbot 使用 Buildot 实现持续集成 使用基于 Python 的工具实现持续集成的理论与实践 牛仔式编码的日子在大多数组织中早 ...
- 如何设置适当的ramp-up period值
如何设置适当的值并不轻易. 首先,假如要使用大量线程的话,ramp-up period 一般不要设置成零. 因为假如设置成零,Jmeter将会在测试的开始就建立全部线程并立即发送访问请求, 这样一来就 ...
- [Web API] 如何让 Web API 统一回传格式以及例外处理[转]
[Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...
- subclipse svn 在64位win7下报Failed to load JavaHL Library
- bzoj 3997 [TJOI2015]组合数学(DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...
- mybatis系列-07-输出映射
7.1 resultType 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功. 如果查询出来的列名和pojo中的属性名全部不一致,没有创建 ...
- Developer Tools(开发工具)
Google提供了使用Java和Python开发App Engine的免费工具.你可以从Google的网站上下载你所用语言和操作系统的软件开发包.Java用户可以以Eclipse集成开发环境的方式获取 ...