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 ...
随机推荐
- hisi平台mii网络模式和rmii网络模式的uboot制作
MII网络uboot编译说明 一:编译生成默认的uboot1. 进入到uboot目录a. cd /home/satan/Hi3518_SDK_V1.0.7.0/osdrv/uboot2. 新建临时文件 ...
- wifi详解(四)
1 IOCTL的调用逻辑 之所以要分析这个,是因为上层wpa_supplicant和WIFI驱动打交道的方式,多半是通过ioctl的方式进行的,所以看看它的调用逻辑(这里只列出其主要的调 ...
- 获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下: PackageManager packageManager = this.getPackageMana ...
- Minimax Triangulation
题意: 按顺序给定一些点,把这些点分割为n - 2个三角形,花费为最大三角形面积,求最小花费 分析: 区间dp,dp[i][j]表示完成区间[i,j]最小花费,dp[i][j]=min(dp[i][j ...
- 《Python 学习手册4th》 第十六章 函数基础
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- Apache benchmark对网站进行压力测试
Apache Benchmark下载:http://down.tech.sina.com.cn/page/3132.html ab 的全称是 ApacheBench , 是 Apache 附带的一个小 ...
- CSS书写规范、顺序
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经验总 ...
- 多线程下OpenCV操作的问题
问题:在OpenCV中,使用cvCaptureFromAVI打开一个视频文件后,并使用cvReleaseCapture释放关闭它后.再开启一个线程使用cvCaptureFromAVI打开一个视频文件, ...
- 线性方法用于Binary clssification
到现在,我们已经学过三种线性方法:linear classification.Linear Regression.logistic Regression.这三种方法的核心都是,不同点在于:最小化的er ...
- Cloudera CDH5 部署实战指南(离线安装)
配置软件源服务器 1.安装createreporpm -ivh deltarpm-3.5-0.5.20090913git.el6.x86_64.rpm rpm -ivh python-deltarpm ...